Skip to content

Commit

Permalink
fix: compare value of inherited getter on test failure (jestjs#10167)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenceSzalai committed Mar 14, 2023
1 parent 94c06ef commit 7eeacae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ test('convert accessor descriptor into value descriptor', () => {
});
});

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

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

test('copies value of inherited getters', () => {
class Foo {
#foo = 42;
get foo() {
return this.#foo;
}
}
const obj = new Foo();

expect(deepCyclicCopyReplaceable(obj).foo).toBe(42);
});

test('copies arrays as array objects', () => {
const array = [null, 42, 'foo', 'bar', [], {}];

Expand Down
8 changes: 5 additions & 3 deletions packages/jest-matcher-utils/src/deepCyclicCopyReplaceable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ export default function deepCyclicCopyReplaceable<T>(

function deepCyclicCopyObject<T>(object: T, cycles: WeakMap<any, unknown>): T {
const newObject = Object.create(Object.getPrototypeOf(object));
const descriptors: {
[x: string]: PropertyDescriptor;
} = Object.getOwnPropertyDescriptors(object);
let descriptors: Record<string, PropertyDescriptor> = {};
let obj = object;
do {
descriptors = Object.assign({}, Object.getOwnPropertyDescriptors(obj), descriptors)
} while ( (obj = Object.getPrototypeOf( obj )) && obj !== Object.getPrototypeOf({}) );

cycles.set(object, newObject);

Expand Down

0 comments on commit 7eeacae

Please sign in to comment.