-
-
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
eval() in code being tested not working in Jest. #5936
Comments
Updated the post to include a reproduction (https://github.com/trusktr/lowclass/tree/jest-issue-5936) |
… has shared build/test/publish configuration for my JS packages. Am not hooking into the builder Jest testing though, am running the test file manually at the moment because Jest seems to have a bug when eval() is used (see jestjs/jest#5936)
To fix the problem on my end, I changed the code so it uses if ( className ) Ctor = new Function( `return function ${ className }() {}` )() This seems to be nicer anyways because I don't need anything from outer scope, and because the code I am evaluating is my own code in my own scope (not third party code) I can easily pass arguments into the |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
eval()
inside code being tested with Jest doesn't work. Does it have to do withvm.runInContext
? (see #439)reproduction
I have some code like this in a source file of mine which some tests are triggering:
Reproduction: https://github.com/trusktr/lowclass/tree/jest-issue-5936
Check out that branch, then:
To see the error run
npm i npm test
To see the code working fine under normal execution, run
Note,
npm test
runs Jest withbuilder
,To run the Jest test manually (for ease debugging), you can run this command:
There is already a
debugger
statement on the line before the problem, you'll have to click play a few times until the first time that the code steps into the part of the conditional whenclassName
is truthy, and the value ofclassName
will be"Foo"
.You can also place a conditional breakpoint on that line if you're familiar with Chrome inspector (right click the line number, click "Add conditional breakpoint...", then input
"className === "Foo"
)What is the expected behavior?
My test file currently does not use any libraries, only
console.assert()
, so I can run it withnode test.js
.When I run it with
node test.js
, it works great!When I run the test with Jest, it fails:
The
eval()
should work. But when the code reaches that part, theeval()
seems to be running in some other context (other scope).So what I see happen is this:
I was expecting
Ctor
to be assigned a function, and therefore no error in the execution of the test file.Again, this all works fine under normal Node execution of the file, and in browsers, just not in Jest.
Please provide your exact Jest configuration
Run
npx envinfo --preset jest
in your project directory and paste the results hereJest is not installed globally
The text was updated successfully, but these errors were encountered: