function-evaluation-order.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. describe("CallExpression", () => {
  2. test("callee is evaluated before arguments", () => {
  3. function foo() {}
  4. const values = [];
  5. [foo][(values.push("callee"), 0)](values.push("args"));
  6. expect(values).toEqual(["callee", "args"]);
  7. });
  8. test("arguments are evaluated in order", () => {
  9. function foo() {}
  10. const values = [];
  11. foo(values.push("arg1"), values.push("arg2"), values.push("arg3"));
  12. expect(values).toEqual(["arg1", "arg2", "arg3"]);
  13. });
  14. test("arguments are evaluated before callee is checked for its type", () => {
  15. const values = [];
  16. expect(() => {
  17. "foo"(values.push("args"));
  18. }).toThrowWithMessage(TypeError, "foo is not a function");
  19. expect(values).toEqual(["args"]);
  20. expect(() => {
  21. "foo"(bar);
  22. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  23. });
  24. });
  25. describe("NewExpression", () => {
  26. test("callee is evaluated before arguments", () => {
  27. function Foo() {}
  28. const values = [];
  29. new [Foo][(values.push("callee"), 0)](values.push("args"));
  30. expect(values).toEqual(["callee", "args"]);
  31. });
  32. test("arguments are evaluated in order", () => {
  33. function Foo() {}
  34. const values = [];
  35. new Foo(values.push("arg1"), values.push("arg2"), values.push("arg3"));
  36. expect(values).toEqual(["arg1", "arg2", "arg3"]);
  37. });
  38. test("arguments are evaluated before callee is checked for its type", () => {
  39. const values = [];
  40. expect(() => {
  41. new "Foo"(values.push("args"));
  42. }).toThrowWithMessage(TypeError, "Foo is not a constructor");
  43. expect(values).toEqual(["args"]);
  44. expect(() => {
  45. new "Foo"(bar);
  46. }).toThrowWithMessage(ReferenceError, "'bar' is not defined");
  47. });
  48. });