Object.defineProperty.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. describe("normal functionality", () => {
  2. let s = Symbol("foo");
  3. test("non-configurable string property", () => {
  4. let o = {};
  5. Object.defineProperty(o, "foo", { value: 1, writable: false, enumerable: false });
  6. expect(o.foo).toBe(1);
  7. o.foo = 2;
  8. expect(o.foo).toBe(1);
  9. expect(o).not.toHaveConfigurableProperty("foo");
  10. expect(o).not.toHaveEnumerableProperty("foo");
  11. expect(o).not.toHaveWritableProperty("foo");
  12. expect(o).toHaveValueProperty("foo", 1);
  13. });
  14. test("non-configurable symbol property", () => {
  15. let o = {};
  16. Object.defineProperty(o, s, { value: 1, writable: false, enumerable: false });
  17. expect(o[s]).toBe(1);
  18. o[s] = 2;
  19. expect(o[s]).toBe(1);
  20. expect(o).not.toHaveConfigurableProperty(s);
  21. expect(o).not.toHaveEnumerableProperty(s);
  22. expect(o).not.toHaveWritableProperty(s);
  23. expect(o).toHaveValueProperty(s, 1);
  24. });
  25. test("array index getter", () => {
  26. let o = {};
  27. Object.defineProperty(o, 2, {
  28. get() {
  29. return 10;
  30. },
  31. });
  32. expect(o[2]).toBe(10);
  33. });
  34. test("symbol property getter", () => {
  35. let o = {};
  36. Object.defineProperty(o, s, {
  37. get() {
  38. return 10;
  39. },
  40. });
  41. expect(o[s]).toBe(10);
  42. });
  43. test("configurable string property", () => {
  44. let o = {};
  45. Object.defineProperty(o, "foo", { value: "hi", writable: true, enumerable: true });
  46. expect(o.foo).toBe("hi");
  47. o.foo = "ho";
  48. expect(o.foo).toBe("ho");
  49. expect(o).not.toHaveConfigurableProperty("foo");
  50. expect(o).toHaveEnumerableProperty("foo");
  51. expect(o).toHaveWritableProperty("foo");
  52. expect(o).toHaveValueProperty("foo", "ho");
  53. });
  54. test("configurable symbol property", () => {
  55. let o = {};
  56. Object.defineProperty(o, s, { value: "hi", writable: true, enumerable: true });
  57. expect(o[s]).toBe("hi");
  58. o[s] = "ho";
  59. expect(o[s]).toBe("ho");
  60. expect(o).not.toHaveConfigurableProperty(s);
  61. expect(o).toHaveEnumerableProperty(s);
  62. expect(o).toHaveWritableProperty(s);
  63. expect(o).toHaveValueProperty(s, "ho");
  64. });
  65. test("reconfigure configurable string property", () => {
  66. let o = {};
  67. Object.defineProperty(o, "foo", { value: 9, configurable: true, writable: false });
  68. Object.defineProperty(o, "foo", { configurable: true, writable: true });
  69. expect(o).toHaveConfigurableProperty("foo");
  70. expect(o).toHaveWritableProperty("foo");
  71. expect(o).not.toHaveEnumerableProperty("foo");
  72. expect(o).toHaveValueProperty("foo", 9);
  73. });
  74. test("reconfigure configurable symbol property", () => {
  75. let o = {};
  76. Object.defineProperty(o, s, { value: 9, configurable: true, writable: false });
  77. Object.defineProperty(o, s, { configurable: true, writable: true });
  78. expect(o).toHaveConfigurableProperty(s);
  79. expect(o).toHaveWritableProperty(s);
  80. expect(o).not.toHaveEnumerableProperty(s);
  81. expect(o).toHaveValueProperty(s, 9);
  82. });
  83. test("define string accessor", () => {
  84. let o = {};
  85. Object.defineProperty(o, "foo", {
  86. configurable: true,
  87. get() {
  88. return o.secret_foo + 1;
  89. },
  90. set(value) {
  91. this.secret_foo = value + 1;
  92. },
  93. });
  94. o.foo = 10;
  95. expect(o.foo).toBe(12);
  96. o.foo = 20;
  97. expect(o.foo).toBe(22);
  98. Object.defineProperty(o, "foo", { configurable: true, value: 4 });
  99. expect(o.foo).toBe(4);
  100. expect((o.foo = 5)).toBe(5);
  101. expect((o.foo = 4)).toBe(4);
  102. });
  103. test("define symbol accessor", () => {
  104. let o = {};
  105. Object.defineProperty(o, s, {
  106. configurable: true,
  107. get() {
  108. return o.secret_foo + 1;
  109. },
  110. set(value) {
  111. this.secret_foo = value + 1;
  112. },
  113. });
  114. o[s] = 10;
  115. expect(o[s]).toBe(12);
  116. o[s] = 20;
  117. expect(o[s]).toBe(22);
  118. Object.defineProperty(o, s, { configurable: true, value: 4 });
  119. expect(o[s]).toBe(4);
  120. expect((o[s] = 5)).toBe(5);
  121. expect((o[s] = 4)).toBe(4);
  122. });
  123. test("issue #3735, reconfiguring property in unique shape", () => {
  124. const o = {};
  125. // In LibJS an object with more than 100 properties gets a unique shape
  126. for (let i = 0; i < 101; ++i) {
  127. o[`property${i}`] = i;
  128. }
  129. Object.defineProperty(o, "x", { configurable: true });
  130. Object.defineProperty(o, "x", { configurable: false });
  131. });
  132. });
  133. describe("errors", () => {
  134. test("redefine non-configurable property", () => {
  135. let o = {};
  136. Object.defineProperty(o, "foo", { value: 1, writable: true, enumerable: true });
  137. expect(() => {
  138. Object.defineProperty(o, "foo", { value: 2, writable: true, enumerable: false });
  139. }).toThrowWithMessage(TypeError, "Object's [[DefineOwnProperty]] method returned false");
  140. });
  141. test("redefine non-configurable symbol property", () => {
  142. let o = {};
  143. let s = Symbol("foo");
  144. Object.defineProperty(o, s, { value: 1, writable: true, enumerable: true });
  145. expect(() => {
  146. Object.defineProperty(o, s, { value: 2, writable: true, enumerable: false });
  147. }).toThrowWithMessage(TypeError, "Object's [[DefineOwnProperty]] method returned false");
  148. });
  149. test("cannot define 'value' and 'get' in the same descriptor", () => {
  150. let o = {};
  151. expect(() => {
  152. Object.defineProperty(o, "a", {
  153. get() {},
  154. value: 9,
  155. });
  156. }).toThrowWithMessage(
  157. TypeError,
  158. "Accessor property descriptor cannot specify a value or writable key"
  159. );
  160. });
  161. test("cannot define 'value' and 'set' in the same descriptor", () => {
  162. let o = {};
  163. expect(() => {
  164. Object.defineProperty(o, "a", {
  165. set() {},
  166. writable: true,
  167. });
  168. }).toThrowWithMessage(
  169. TypeError,
  170. "Accessor property descriptor cannot specify a value or writable key"
  171. );
  172. });
  173. test("redefine non-configurable accessor", () => {
  174. let o = {};
  175. Object.defineProperty(o, "foo", {
  176. configurable: false,
  177. get() {
  178. return this.secret_foo + 2;
  179. },
  180. set(value) {
  181. o.secret_foo = value + 2;
  182. },
  183. });
  184. expect(() => {
  185. Object.defineProperty(o, "foo", {
  186. configurable: false,
  187. get() {
  188. return this.secret_foo + 2;
  189. },
  190. });
  191. }).toThrowWithMessage(TypeError, "Object's [[DefineOwnProperty]] method returned false");
  192. });
  193. });