Skip to content
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

Closed
trusktr opened this issue Apr 6, 2018 · 5 comments
Closed

eval() in code being tested not working in Jest. #5936

trusktr opened this issue Apr 6, 2018 · 5 comments
Labels

Comments

@trusktr
Copy link

trusktr commented Apr 6, 2018

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 with vm.runInContext? (see #439)

reproduction

I have some code like this in a source file of mine which some tests are triggering:

            let Ctor

            if ( className ) eval( `Ctor = function ${ className }() {}` )
            else {
                // anonymous even in ES6+
                Ctor = ( () => function() {} )()
            }

            Ctor.prototype = { __proto__: Object.prototype, constructor: Ctor }

            return Ctor

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

node src/test.js

Note, npm test runs Jest with builder,

To run the Jest test manually (for ease debugging), you can run this command:

 node --inspect-brk ./node_modules/.bin/jest --config ./node_modules/builder-js-package/config/jest.config.js

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 when className is truthy, and the value of className 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 with node 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, the eval() seems to be running in some other context (other scope).

So what I see happen is this:

            let Ctor

            // Ctor is undefined here

            eval( `Ctor = function ${ className }() {}` )

            // Ctor is still undefined here

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

module.exports = {
    rootDir: process.cwd(),

    transform: {
        '^.+\\.js$': path.resolve(__dirname, 'babel-jest'),
    },

    // a great environment that supports Custom Elements
    testEnvironment: '@skatejs/ssr/jest',
}

Run npx envinfo --preset jest in your project directory and paste the results here

❯ npx envinfo --preset jest
npx: installed 1 in 2.41s

  System:
    OS: Linux 3.18
    CPU: x64 Intel(R) Core(TM) m3-6Y30 CPU @ 0.90GHz
  Binaries:
    Node: 9.9.0
    Yarn: 1.3.2
    npm: 5.8.0

❯ cat node_modules/jest/package.json | grep version
  "version": "22.4.3"

Jest is not installed globally

@trusktr
Copy link
Author

trusktr commented Apr 6, 2018

Updated the post to include a reproduction (https://github.com/trusktr/lowclass/tree/jest-issue-5936)

trusktr added a commit to trusktr/lowclass that referenced this issue Apr 6, 2018
… 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)
@trusktr
Copy link
Author

trusktr commented Apr 6, 2018

To fix the problem on my end, I changed the code so it uses Function instead of eval:

            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 new Function if needed. Well, I can easily pass specific arguments in for third party code too. So, Function seems better overall and more secure.

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

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.

@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant