Skip to content

Commit

Permalink
fix: correctly access getters in deepCyclicCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Aug 20, 2024
1 parent 1eca335 commit 2ecfd4e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
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

0 comments on commit 2ecfd4e

Please sign in to comment.