forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: failed expect with circular references hangs jest worker
relates jestjs#10577
- Loading branch information
Pavel Chertorogov
committed
Nov 2, 2023
1 parent
544a4b8
commit 703cfad
Showing
6 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/jest-worker/src/workers/__tests__/withoutCircularRefs.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {withoutCircularRefs} from '../withoutCircularRefs'; | ||
|
||
it('test simple values', () => { | ||
expect(withoutCircularRefs(undefined)).toBeUndefined(); | ||
expect(withoutCircularRefs(null)).toBeNull(); | ||
expect(withoutCircularRefs(0)).toBe(0); | ||
expect(withoutCircularRefs('12')).toBe('12'); | ||
expect(withoutCircularRefs(true)).toBe(true); | ||
expect(withoutCircularRefs([1])).toEqual([1]); | ||
expect(withoutCircularRefs({a: 1, b: {c: 2}})).toEqual({a: 1, b: {c: 2}}); | ||
}); | ||
|
||
it('test circular values', () => { | ||
const circular = {self: undefined as any}; | ||
circular.self = circular; | ||
|
||
expect(withoutCircularRefs(circular)).toEqual({self: '[Circular]'}); | ||
|
||
expect(withoutCircularRefs([{a: circular, b: null}])).toEqual([ | ||
{a: {self: '[Circular]'}, b: null}, | ||
]); | ||
|
||
expect(withoutCircularRefs({a: {b: circular}, c: undefined})).toEqual({ | ||
a: {b: {self: '[Circular]'}, c: undefined}, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export function withoutCircularRefs(obj: unknown): unknown { | ||
const cache = new WeakSet(); | ||
function copy(obj: unknown) { | ||
if (typeof obj !== 'object' || obj === null) { | ||
return obj; | ||
} | ||
if (cache.has(obj)) { | ||
return '[Circular]'; | ||
} | ||
cache.add(obj); | ||
const copyObj: any = Array.isArray(obj) ? [] : {}; | ||
for (const key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
copyObj[key] = copy((obj as any)[key]); | ||
} | ||
} | ||
return copyObj; | ||
} | ||
return copy(obj); | ||
} |