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 91e8082 commit 25db46b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
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 25db46b

Please sign in to comment.