Skip to content

Commit

Permalink
fix(jest-matcher-utils): compare value of inherited getter on test fa…
Browse files Browse the repository at this point in the history
…ilure (#10167) (#14007)
  • Loading branch information
BenceSzalai authored Mar 20, 2023
1 parent a95eeb6 commit 04f11d8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- `[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))
- `[jest-matcher-utils]` Fix copying value of inherited getters ([#14007](https://github.com/facebook/jest/pull/14007))

### Chore & Maintenance

Expand Down
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
15 changes: 12 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,18 @@ 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 04f11d8

Please sign in to comment.