Proxy.handler-ownKeys.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // TODO: Add "[[OwnPropertyKeys]] trap normal behavior" tests
  2. describe("[[OwnPropertyKeys]] invariants", () => {
  3. // TODO: Add tests for other [[OwnPropertyKeys]] trap invariants
  4. test("cannot report new property of non-extensible object", () => {
  5. const target = Object.preventExtensions({});
  6. const handler = {
  7. ownKeys() {
  8. return ["foo", "bar", "baz"];
  9. },
  10. };
  11. const proxy = new Proxy(target, handler);
  12. expect(() => {
  13. Reflect.ownKeys(proxy);
  14. }).toThrowWithMessage(
  15. TypeError,
  16. "Proxy handler's ownKeys trap violates invariant: cannot report new property 'foo' of non-extensible object"
  17. );
  18. });
  19. test("cannot skip property of non-extensible object", () => {
  20. const target = Object.preventExtensions({ foo: null });
  21. const handler = {
  22. ownKeys() {
  23. return ["bar", "baz"];
  24. },
  25. };
  26. const proxy = new Proxy(target, handler);
  27. expect(() => {
  28. Reflect.ownKeys(proxy);
  29. }).toThrowWithMessage(
  30. TypeError,
  31. "Proxy handler's ownKeys trap violates invariant: cannot skip property 'foo' of non-extensible object"
  32. );
  33. });
  34. test("cannot skip non-configurable property", () => {
  35. const target = Object.defineProperty({}, "foo", { configurable: false });
  36. const handler = {
  37. ownKeys() {
  38. return ["bar", "baz"];
  39. },
  40. };
  41. const proxy = new Proxy(target, handler);
  42. expect(() => {
  43. Reflect.ownKeys(proxy);
  44. }).toThrowWithMessage(
  45. TypeError,
  46. "Proxy handler's ownKeys trap violates invariant: cannot skip non-configurable property 'foo'"
  47. );
  48. });
  49. });