Skip to content

Commit 04f11d8

Browse files
authored
fix(jest-matcher-utils): compare value of inherited getter on test failure (#10167) (#14007)
1 parent a95eeb6 commit 04f11d8

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

77
- `[jest-environment-jsdom, jest-environment-node]` Fix assignment of `customExportConditions` via `testEnvironmentOptions` when custom env subclass defines a default value ([#13989](https://github.com/facebook/jest/pull/13989))
8+
- `[jest-matcher-utils]` Fix copying value of inherited getters ([#14007](https://github.com/facebook/jest/pull/14007))
89

910
### Chore & Maintenance
1011

packages/jest-matcher-utils/src/__tests__/deepCyclicCopyReplaceable.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ test('convert accessor descriptor into value descriptor', () => {
4343
});
4444
});
4545

46-
test('should not skips non-enumerables', () => {
46+
test('should not skip non-enumerables', () => {
4747
const obj = {};
4848
Object.defineProperty(obj, 'foo', {enumerable: false, value: 'bar'});
4949

@@ -66,6 +66,18 @@ test('copies symbols', () => {
6666
expect(deepCyclicCopyReplaceable(obj)[symbol]).toBe(42);
6767
});
6868

69+
test('copies value of inherited getters', () => {
70+
class Foo {
71+
#foo = 42;
72+
get foo() {
73+
return this.#foo;
74+
}
75+
}
76+
const obj = new Foo();
77+
78+
expect(deepCyclicCopyReplaceable(obj).foo).toBe(42);
79+
});
80+
6981
test('copies arrays as array objects', () => {
7082
const array = [null, 42, 'foo', 'bar', [], {}];
7183

packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,18 @@ export default function deepCyclicCopyReplaceable<T>(
5757

5858
function deepCyclicCopyObject<T>(object: T, cycles: WeakMap<any, unknown>): T {
5959
const newObject = Object.create(Object.getPrototypeOf(object));
60-
const descriptors: {
61-
[x: string]: PropertyDescriptor;
62-
} = Object.getOwnPropertyDescriptors(object);
60+
let descriptors: Record<string, PropertyDescriptor> = {};
61+
let obj = object;
62+
do {
63+
descriptors = Object.assign(
64+
{},
65+
Object.getOwnPropertyDescriptors(obj),
66+
descriptors,
67+
);
68+
} while (
69+
(obj = Object.getPrototypeOf(obj)) &&
70+
obj !== Object.getPrototypeOf({})
71+
);
6372

6473
cycles.set(object, newObject);
6574

0 commit comments

Comments
 (0)