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: correctly copy getters in deepCyclicCopy #15265

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -84,6 +84,7 @@
- [**BREAKING**] `--testPathPattern` is now `--testPathPatterns`
- [**BREAKING**] Specifying `testPathPatterns` when programmatically calling `watch` must be specified as `new TestPathPatterns(patterns)`, where `TestPathPatterns` can be imported from `@jest/pattern`
- `[jest-reporters, jest-runner]` Unhandled errors without stack get correctly logged to console ([#14619](https://github.com/jestjs/jest/pull/14619))
- `[jest-util]` Correctly copy getters in `deepCyclicCopy` ([#15265](https://github.com/jestjs/jest/pull/15265))
- `[jest-worker]` Properly handle a circular reference error when worker tries to send an assertion fails where either the expected or actual value is circular ([#15191](https://github.com/jestjs/jest/pull/15191))
- `[jest-worker]` Properly handle a BigInt when worker tries to send an assertion fails where either the expected or actual value is BigInt ([#15191](https://github.com/jestjs/jest/pull/15191))

Expand Down
7 changes: 7 additions & 0 deletions packages/jest-util/src/__tests__/deepCyclicCopy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ it('does not execute getters/setters, but copies them', () => {
expect(fn).not.toHaveBeenCalled();
});

test('keeps error stack getter', () => {
const obj = new Error('hello');
const copy = deepCyclicCopy(obj);

expect(obj.stack).toBe(copy.stack);
});

it('copies symbols', () => {
const symbol = Symbol('foo');
const obj = {[symbol]: 42};
Expand Down
13 changes: 11 additions & 2 deletions packages/jest-util/src/deepCyclicCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,28 @@ function deepCyclicCopyObject<T>(

cycles.set(object, newObject);

for (const key of Object.keys(descriptors)) {
for (const [key, descriptor] of Object.entries(descriptors)) {
if (options.blacklist && options.blacklist.has(key)) {
delete descriptors[key];
continue;
}

const descriptor = descriptors[key];
if (descriptor.value !== undefined) {
descriptor.value = deepCyclicCopy(
descriptor.value,
{blacklist: EMPTY, keepPrototype: options.keepPrototype},
cycles,
);
} else if (typeof descriptor.get === 'function') {
descriptor.get = () => {
const value: unknown = (object as any)[key];

return deepCyclicCopy(
value,
{blacklist: EMPTY, keepPrototype: options.keepPrototype},
cycles,
);
};
}

descriptor.configurable = true;
Expand Down
Loading