with-basic.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. test("basic with statement functionality", () => {
  2. var object = { foo: 5, bar: 6, baz: 7 };
  3. var qux = 1;
  4. var bar = 99;
  5. with (object) {
  6. expect(foo).toBe(5);
  7. expect(bar).toBe(6);
  8. expect(baz).toBe(7);
  9. expect(qux).toBe(1);
  10. expect(typeof quz).toBe("undefined");
  11. bar = 2;
  12. }
  13. expect(object.bar).toBe(2);
  14. expect(() => foo).toThrowWithMessage(ReferenceError, "'foo' is not defined");
  15. expect(bar).toBe(99);
  16. });
  17. test("syntax error in strict mode", () => {
  18. expect("'use strict'; with (foo) {}").not.toEval();
  19. });
  20. test("restores lexical environment even when exception is thrown", () => {
  21. var object = {
  22. foo: 1,
  23. get bar() {
  24. throw Error();
  25. },
  26. };
  27. try {
  28. with (object) {
  29. expect(foo).toBe(1);
  30. bar;
  31. }
  32. expect().fail();
  33. } catch (e) {
  34. expect(() => foo).toThrowWithMessage(ReferenceError, "'foo' is not defined");
  35. }
  36. expect(() => foo).toThrowWithMessage(ReferenceError, "'foo' is not defined");
  37. });