Skip to content
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(jest-matcher-utils): compare value of inherited getter on test failure (#10167) #14007

Merged
merged 3 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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