-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: drop getters and setters when diffing objects for error #9757
Changes from 10 commits
2a33596
4dcb197
395abcd
3503d77
b14a485
cf56846
8f5824c
3aed077
9ff198f
5f72f4e
30495ca
fe41787
1aea89b
c0edc89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,20 +20,6 @@ test('returns the same value for primitive or function values', () => { | |
expect(deepCyclicCopyReplaceable(fn)).toBe(fn); | ||
}); | ||
|
||
test('does not execute getters/setters, but copies them', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it's not just in the comments - we invoke the getters on purpose now, so this test doesn't make sense anymore |
||
const fn = jest.fn(); | ||
const obj = { | ||
// @ts-ignore | ||
get foo() { | ||
fn(); | ||
}, | ||
}; | ||
const copy = deepCyclicCopyReplaceable(obj); | ||
|
||
expect(Object.getOwnPropertyDescriptor(copy, 'foo')).toBeDefined(); | ||
expect(fn).not.toBeCalled(); | ||
}); | ||
|
||
test('copies symbols', () => { | ||
const symbol = Symbol('foo'); | ||
const obj = {[symbol]: 42}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,21 +49,22 @@ export default function deepCyclicCopyReplaceable<T>( | |
|
||
function deepCyclicCopyObject<T>(object: T, cycles: WeakMap<any, any>): T { | ||
const newObject = Object.create(Object.getPrototypeOf(object)); | ||
const descriptors = Object.getOwnPropertyDescriptors(object); | ||
const descriptors: { | ||
[x: string]: PropertyDescriptor; | ||
} = Object.getOwnPropertyDescriptors(object); | ||
|
||
cycles.set(object, newObject); | ||
|
||
Object.keys(descriptors).forEach(key => { | ||
const descriptor = descriptors[key]; | ||
if (typeof descriptor.value !== 'undefined') { | ||
descriptor.value = deepCyclicCopyReplaceable(descriptor.value, cycles); | ||
} | ||
|
||
if (!('set' in descriptor)) { | ||
descriptor.writable = true; | ||
} | ||
|
||
descriptor.configurable = true; | ||
descriptors[key] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can the enumerable check be here instead? if (descriptors[key].enumerable) {
descriptors[key] = ...
} else {
delete descriptors[key];
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At least if I understood your comment correcly diff --git i/packages/jest-matcher-utils/src/Replaceable.ts w/packages/jest-matcher-utils/src/Replaceable.ts
index fc8c7343c..dd9e04f32 100644
--- i/packages/jest-matcher-utils/src/Replaceable.ts
+++ w/packages/jest-matcher-utils/src/Replaceable.ts
@@ -35,12 +35,6 @@ export default class Replaceable {
Object.entries(this.object).forEach(([key, value]) => {
cb(value, key, this.object);
});
- Object.getOwnPropertySymbols(this.object).forEach(key => {
- const descriptor = Object.getOwnPropertyDescriptor(this.object, key);
- if (descriptor?.enumerable) {
- cb(this.object[key], key, this.object);
- }
- });
} else {
this.object.forEach(cb);
}
diff --git i/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts w/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts
index ad98131dd..67271d5d8 100644
--- i/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts
+++ w/packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts
@@ -56,15 +56,19 @@ function deepCyclicCopyObject<T>(object: T, cycles: WeakMap<any, any>): T {
cycles.set(object, newObject);
Object.keys(descriptors).forEach(key => {
- descriptors[key] = {
- configurable: true,
- enumerable: true,
- value: deepCyclicCopyReplaceable(
- (object as Record<string, unknown>)[key],
- cycles,
- ),
- writable: true,
- };
+ if (descriptors[key].enumerable) {
+ descriptors[key] = descriptors[key] = {
+ configurable: true,
+ enumerable: true,
+ value: deepCyclicCopyReplaceable(
+ (object as Record<string, unknown>)[key],
+ cycles,
+ ),
+ writable: true,
+ };
+ } else {
+ delete descriptors[key];
+ }
});
return Object.defineProperties(newObject, descriptors); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'd still need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right! done 🙂 |
||
configurable: true, | ||
enumerable: true, | ||
value: deepCyclicCopyReplaceable( | ||
(object as Record<string, unknown>)[key], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
cycles, | ||
), | ||
writable: true, | ||
}; | ||
}); | ||
|
||
return Object.defineProperties(newObject, descriptors); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI is failing on master due to this missing newline, snuck it in here