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

jest 0.5.x: eval strict function and new expression #439

Closed
DmitrySoshnikov opened this issue Jul 25, 2015 · 6 comments
Closed

jest 0.5.x: eval strict function and new expression #439

DmitrySoshnikov opened this issue Jul 25, 2015 · 6 comments

Comments

@DmitrySoshnikov
Copy link
Contributor

With vm.runInContext and jsdom 5.4.3, we started to get ReferenceError issues:

describe('strict eval', function() {
  it('eval', function() {
    var code = [
      'var _bar = 10;',
      'function foo() {',
      '  "use strict";', // because of this
      '  return _bar;',
      '}',
    ].join('\n');

    eval(code);

    new foo(); // Reference error: _bar is not defined
  });
});

Thus it relates only to "use strict" inside the function, without it, _bar is available in the new foo() call.

The vm.runInContext seems works fine in isolation in this case. Can be jsdom related?

CC @ide, @domenic

@DmitrySoshnikov
Copy link
Contributor Author

OK, it's nevertheless issue of vm.runInContext. If a code is wrapped into a function, then evaling strict function inside that function, doesn't capture the bindings.

This works (file test.js):

var code = `
var foo = {m: 1};

function bar() {
  "use strict";
  return foo;
}
`;

eval(code);

bar();

Node session:

> vm.runInNewContext(fs.readFileSync('/Users/dmitrys/test.js', 'utf-8'))
{ m: 1 }
>

This doesn't:

function main() {

  var code = `
    var foo = {m: 1};

    function bar() {
      "use strict";
      return foo;
    }
  `;

  eval(code);

  bar(); // foo is not defined

}

main();

Node session:

> vm.runInNewContext(fs.readFileSync('/Users/dmitrys/test.js', 'utf-8'))
undefined:6
      return foo;
             ^
ReferenceError: foo is not defined
    at bar (eval at main (evalmachine.<anonymous>:13:8), <anonymous>:6:14)
    at main (evalmachine.<anonymous>:15:3)
    at evalmachine.<anonymous>:19:1
    at ContextifyScript.Script.runInNewContext (vm.js:18:15)
    at Object.exports.runInNewContext (vm.js:49:17)
    at repl:1:4
    at REPLServer.defaultEval (repl.js:154:27)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:308:12)

Unless I'm missing something obvious from strict mode, it's probably a bug in vm module, and is not related to Jest or jsdom.

@ide
Copy link
Contributor

ide commented Jul 25, 2015

The exact same script source is passed to vm.runInContext and jsdom's evaluator: https://github.com/facebook/jest/blob/0.5.x/src/JSDomEnvironment.js#L110 and https://github.com/facebook/jest/blob/0.5.x/src/JSDomEnvironment.js#L132. And looking how jsdom evaluate scripts, it also uses vm.runInContext.

I repro'd your latest results where the same error occurs with both vm.runInContext and jsdom, even with plain function calls without new.

Another thing I noticed was that after the eval call, expect(_bar).toBe(10) throws an error since _bar is not defined. So either the eval is acting as some kind of indirect eval or it is using a different global scope.

@DmitrySoshnikov
Copy link
Contributor Author

Need to notice, this is test form io.js v2.3.1. With Node v.0.12 the vm.runInContext has even different behavior: the bar is not defined there.

Also, if no "strict mode" is used, then the foo is captured in bar.

@DmitrySoshnikov
Copy link
Contributor Author

OK, I filed this issue on io.js yet: nodejs/node#2245, hope we'll get some clarifications. Though, it's currently a blocker for us at Facebook to switch to Jest 0.5 with io.js (CC @yungsters), and probably will be a blocker to merge 0.5 to master on August 1st as we planned.

@cpojer
Copy link
Member

cpojer commented Oct 21, 2015

This seems like it was fixed in node 4.

@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 May 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@cpojer @DmitrySoshnikov @ide and others