-
-
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-runner): Handle unsafe worker_threads structureClone
with function
type in matchers
#14436
Open
dj-stormtrooper
wants to merge
8
commits into
jestjs:main
Choose a base branch
from
dj-stormtrooper:fix/worker-threads-data-clone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a2a54a0
fix: Additional error wrapper for `postMessage` to avoid unhandled `D…
dj-stormtrooper 627031c
fix: Handle unsafe `structureClone` with `functions` in matchers whil…
dj-stormtrooper 3a3295f
chore: Changelog update
dj-stormtrooper 76791cc
chore: Changelog bump
dj-stormtrooper e0f991a
Merge branch 'main' into fix/worker-threads-data-clone
dj-stormtrooper 9db8b2e
Merge branch 'main' into fix/worker-threads-data-clone
dj-stormtrooper ec421c8
Merge branch 'fix/worker-threads-data-clone' of github.com:dj-stormtr…
dj-stormtrooper 3f50677
Merge branch 'main' into fix/worker-threads-data-clone
SimenB 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* 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 {replaceFunctionsWithStringReferences} from '../helpers'; | ||
|
||
it('serialize functions inside the nested object', () => { | ||
const obj = { | ||
foo: () => {}, | ||
nested: { | ||
fn: function bar() { | ||
return 0; | ||
}, | ||
}, | ||
nestedArray: [{baz: function baz() {}}, () => {}], | ||
}; | ||
|
||
expect(replaceFunctionsWithStringReferences(obj)).toEqual({ | ||
foo: '[Function foo]', | ||
nested: { | ||
fn: '[Function bar]', | ||
}, | ||
nestedArray: [ | ||
{ | ||
baz: '[Function baz]', | ||
}, | ||
'[Function anonymous]', | ||
], | ||
}); | ||
}); |
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,29 @@ | ||
/** | ||
* 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 const replaceFunctionsWithStringReferences = <T>(value: T): T => { | ||
if (typeof value === 'function') { | ||
return `[Function ${value.name || 'anonymous'}]` as unknown as T; | ||
} | ||
|
||
if (Array.isArray(value)) { | ||
return value.map(replaceFunctionsWithStringReferences) as T; | ||
} | ||
|
||
const isObject = value !== null && typeof value === 'object'; | ||
if (isObject) { | ||
const oldObject = value as Record<string, unknown>; | ||
const newObject: Record<string, unknown> = {}; | ||
for (const key of Object.keys(value)) { | ||
newObject[key] = replaceFunctionsWithStringReferences(oldObject[key]); | ||
} | ||
|
||
return newObject as T; | ||
} | ||
|
||
return value; | ||
}; |
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
Oops, something went wrong.
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.
Do I get it right: the problem is that
failureDetails
includes data which cannot be serialised? If so, it be better to serialised it earlier in the test runner.This was discussed already, I will try to dig out the thread. The idea is that we know in advance that all what the runner returns has to be serialisable, because that data will be passed through workers. If I remember it right, the test runner is already serialising most of the data. Seem like something was simply overlooked.
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.
Yes, exactly, the problem with
function
type which throw an error during clone. Also DOM nodes might cause similar behaviour, but I'm not sure if it's a valid case for workers.I was also thinking about it, my only concern was that it might be exposed to other tools (outside of
jest
repo). But if the approach is ok I can fix this and add some tests to cover the serialisationSounds good, found this related PR, would be nice to look into other discussions
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.
Here it is. I was talking about #11467. The conclusions are made in #11467 (comment) and following comments.
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.
@mrazauskas Thanks a lot, now I have full context