function-evaluation-order.js 809 B

123456789101112131415161718192021222324252627282930
  1. test("callee is evaluated before arguments", () => {
  2. function foo() {}
  3. const values = [];
  4. [foo][(values.push("callee"), 0)](values.push("args"));
  5. expect(values).toEqual(["callee", "args"]);
  6. });
  7. test("arguments are evaluated in order", () => {
  8. function foo() {}
  9. const values = [];
  10. foo(values.push("arg1"), values.push("arg2"), values.push("arg3"));
  11. expect(values).toEqual(["arg1", "arg2", "arg3"]);
  12. });
  13. test("arguments are evaluated before callee is checked for its type", () => {
  14. const values = [];
  15. expect(() => {
  16. "foo"(values.push("args"));
  17. }).toThrowWithMessage(TypeError, "foo is not a function");
  18. expect(values).toEqual(["args"]);
  19. expect(() => {
  20. "foo"(bar);
  21. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  22. });