Repository navigation
Fix issue #53: Illegal invocation in Jest tests - #164
Merged
verhovsky merged 14 commits intoOct 16, 2023
Merged
Conversation
ilanashapiro
force-pushed
the
fix-53-illegal-invocation
branch
from
September 15, 2023 20:16
1290741 to
de78df6
Compare
ilanashapiro
marked this pull request as ready for review
September 15, 2023 20:19
Contributor
Author
Contributor
Author
|
The tests on Node 10/12 are failing due to the version of Jest (the latest) I have in package.json. I will try to figure out which version of Jest is compatible with Node 10 and up. |
Contributor
Author
|
@verhovsky Could I ask you or a member of your team to take a look at this? Thank you! |
Contributor
Author
|
@verhovsky Just wanted to check in if it would be possible to get a review on this? Thank you! |
verhovsky
reviewed
Oct 16, 2023
verhovsky
reviewed
Oct 16, 2023
verhovsky
reviewed
Oct 16, 2023
Merged
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR fixes #53
This PR is a modification and update to @cellog's PR from a few years ago: #75.
From @cellog:
"Basically, the problem described in https://stackoverflow.com/questions/9677985/uncaught-typeerror-illegal-invocation-in-chrome is what is happening here, but it appears to be a race condition.
When we re-load the context in the same process (which happens with Jest's worker pool), the getter is triggered, for no apparent reason, BEFORE the native extension has had a chance to load. this is actually the user-land object, with no knowledge of the native prototype yet. Just by changing the name of the extension's getters, it fixes this race condition."
When I tried changing the name of the extension's getters, it resulted in all of them being undefined. The solution I found is to only call the prototype's methods if the native extension is fully loaded. When the extension has fully loaded, "this" should be an instance of the class whose prototype we are acting on. For instance, in the following code we are acting on the prototype of Tree:
so "this" should be an instance of Tree. If it is not, the native extension has not fully loaded, so we skip the invocation (hence the if statement). Furthermore, I found that even when the context is correct, the race condition sometimes results in the method in question (here, rootNode) being undefined, so the null method check was also added. Finally, I am adding configurable: true since I found that sometimes, Jest attempts to overwrite properties (specifically, nodeText, but possibly others) on the previous context due to the race condition, and we do not want to throw an error if this happens.
This all results in get() returning
undefinedwhen the native extension isn't fully loaded, so the jest tests need to have a check to reflect this, for instance:There are 3 Jest tests currently in the repo, you can ensure this PR works by simply running
npm test.