-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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-worker): failed assertion with circular value hangs the test run #14675
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b06be16
fix: failed expect with circular references hangs jest worker
nodkz d2ebca4
Merge branch 'main' into fix-child-circular-result
nodkz 9dd6e67
style: add copyright headers
nodkz 14ddff0
style: add comments to the code
nodkz b8c6ba0
enable skipped test "e2e/__tests__/circularInequality.test.ts"
nodkz 8bc7473
style: introduce suggested change
nodkz 8ece12d
fix typo
nodkz d76b9a2
increase timeouts for passing test on github actions
nodkz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
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,27 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we do the inverse as well - rebuild the object with the circular references in place?
I.e. what https://www.npmjs.com/package/flatted does (I see they now wanna push users to https://github.com/ungap/structured-clone/blob/main/README.md#extra-features, might be worth a look)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SimenB
flatted
andstructured-clone
have own serialization format, so both jest-runner and jest-worker should have the same serialize/parse logic. But currently runner and worker are different packages and end-users might have different versions on their installation.So we break a transport layer by this improvement. Better to do it in another PR as improvement and R&D, this PR is a bug fix PR. And I'm very sorry but I don't have enough bandwidth to deal with bumping v30 😉