new-expression.js 997 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // This file must not be formatted by prettier. Make sure your IDE
  2. // respects the .prettierignore file!
  3. test("new-expression parsing", () => {
  4. function Foo() {
  5. this.x = 1;
  6. }
  7. let foo = new Foo();
  8. expect(foo.x).toBe(1);
  9. foo = new Foo
  10. expect(foo.x).toBe(1);
  11. foo = new
  12. Foo
  13. ();
  14. expect(foo.x).toBe(1);
  15. foo = new Foo + 2
  16. expect(foo).toBe("[object Object]2");
  17. });
  18. test("new-expressions with object keys", () => {
  19. let a = {
  20. b: function () {
  21. this.x = 2;
  22. },
  23. };
  24. foo = new a.b();
  25. expect(foo.x).toBe(2);
  26. foo = new a.b;
  27. expect(foo.x).toBe(2);
  28. foo = new
  29. a.b();
  30. expect(foo.x).toBe(2);
  31. });
  32. test("new-expressions with function calls", () => {
  33. function funcGetter() {
  34. return function (a, b) {
  35. this.x = a + b;
  36. };
  37. }
  38. foo = new funcGetter()(1, 5);
  39. expect(foo).toBeUndefined();
  40. foo = new (funcGetter())(1, 5);
  41. expect(foo.x).toBe(6);
  42. });