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

Requiring jsdom throws "Unexpected token ILLEGAL" #128

Closed
Daniel15 opened this Issue Sep 8, 2014 · 7 comments

Comments

Projects
None yet
5 participants
@Daniel15
Copy link
Member

Daniel15 commented Sep 8, 2014

I'm trying to write tests for a module that uses jsdom.

Basic repro: npm install jsdom then put this in a test file:

jest.autoMockOff();

describe('test', function() {
  it('does some stuff', function() {
    var jsdom = require('jsdom').jsdom;
  });
});

This results in an error:

 FAIL  src/__tests__/mytest.js (0.214s)
● test › it does some stuff
  - SyntaxError: /usr/local/var/www/react-magic/node_modules/jsdom/lib/jsdom.js: /usr/local/var/www/react-magic/node_modules/jsdom/lib/jsdom/browser/index.js: /usr/local/var/www/react-magic/node_modules/jsdom/node_modules/contextify/lib/contextify.js: /usr/local/var/www/react-magic/node_modules/jsdom/node_modules/contextify/build/Release/contextify.node: Unexpected token ILLEGAL
        at Contextify.sandbox.run (/usr/local/var/www/react-magic/node_modules/jest-cli/node_modules/jsdom/node_modules/contextify/lib/contextify.js:12:24)
        at JSDomEnvironment.runSourceText (/usr/local/var/www/react-magic/node_modules/jest-cli/src/JSDomEnvironment.js:106:22)
        at Object.runContentWithLocalBindings (/usr/local/var/www/react-magic/node_modules/jest-cli/src/lib/utils.js:299:23)
        at Loader._execModule (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:243:9)
        at Loader.requireModule (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:886:12)
        at Loader.requireModuleOrMock (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:907:17)
        at bindings (/usr/local/var/www/react-magic/node_modules/jsdom/node_modules/contextify/node_modules/bindings/bindings.js:76:44)
        at /usr/local/var/www/react-magic/node_modules/jsdom/node_modules/contextify/lib/contextify.js:1:101
        at Object.runContentWithLocalBindings (/usr/local/var/www/react-magic/node_modules/jest-cli/src/lib/utils.js:315:17)
        at Loader._execModule (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:243:9)
        at Loader.requireModule (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:886:12)
        at Loader.requireModuleOrMock (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:907:17)
        at /usr/local/var/www/react-magic/node_modules/jsdom/lib/jsdom/browser/index.js:14:21
        at Object.runContentWithLocalBindings (/usr/local/var/www/react-magic/node_modules/jest-cli/src/lib/utils.js:315:17)
        at Loader._execModule (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:243:9)
        at Loader.requireModule (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:886:12)
        at Loader.requireModuleOrMock (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:907:17)
        at /usr/local/var/www/react-magic/node_modules/jsdom/lib/jsdom.js:11:20
        at Object.runContentWithLocalBindings (/usr/local/var/www/react-magic/node_modules/jest-cli/src/lib/utils.js:315:17)
        at Loader._execModule (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:243:9)
        at Loader.requireModule (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:886:12)
        at Loader.requireModuleOrMock (/usr/local/var/www/react-magic/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:907:17)
        at Spec.<anonymous> (/usr/local/var/www/react-magic/src/__tests__/mytest.js:5:5)
        at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
1 test failed, 0 test passed (1 total)
Run time: 1.662s

@amasad amasad added the 🐛 Bug label Sep 10, 2014

@danvk

This comment has been minimized.

Copy link

danvk commented Sep 30, 2014

This is a major problem, as many JS libraries have transitive dependencies on jsdom. For example, you can't require('d3') in code being tested with React due to this bug. See my mailing list post.

@jeffmo

This comment has been minimized.

Copy link
Member

jeffmo commented Oct 6, 2014

I hear your pain, but I'm super oversubscribed at the moment and likely won't have time to investigate for a little while :(

If anyone wants to try diving in to this and send a pull request, I'll do everything I can to help get it pulled in.

@danvk

This comment has been minimized.

Copy link

danvk commented Oct 7, 2014

The file with the "Unexpected token ILLEGAL" is node_modules/jsdom/node_modules/contextify/build/Release/contextify.node, which is a binary file. Something (node-haste?) is trying to parse it as JS.

@danvk

This comment has been minimized.

Copy link

danvk commented Oct 7, 2014

Indeed, I can simplify @Daniel15's repro by using contextify directly: npm install contextify and

jest.autoMockOff();

describe('test', function() {
  it('does some stuff', function() {
    var contextify = require('contextify');
  });
});

Running this under Jest gives this error:

Found 1 matching test...
 FAIL  __tests__/simple.js (0.017s)
● test › it does some stuff
  - SyntaxError: /Users/danvk/code/jest-d3-repro/node_modules/contextify/lib/contextify.js: /Users/danvk/code/jest-d3-repro/node_modules/contextify/build/Release/contextify.node: Unexpected token ILLEGAL
        at Contextify.sandbox.run (/Users/danvk/code/jest-d3-repro/node_modules/jest-cli/node_modules/jsdom/node_modules/contextify/lib/contextify.js:12:24)
        at JSDomEnvironment.runSourceText (/Users/danvk/code/jest-d3-repro/node_modules/jest-cli/src/JSDomEnvironment.js:106:22)
        at Object.runContentWithLocalBindings (/Users/danvk/code/jest-d3-repro/node_modules/jest-cli/src/lib/utils.js:299:23)
        at Loader._execModule (/Users/danvk/code/jest-d3-repro/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:243:9)
        at Loader.requireModule (/Users/danvk/code/jest-d3-repro/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:886:12)
        at Loader.requireModuleOrMock (/Users/danvk/code/jest-d3-repro/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:907:17)
        at bindings (/Users/danvk/code/jest-d3-repro/node_modules/contextify/node_modules/bindings/bindings.js:76:44)
        at /Users/danvk/code/jest-d3-repro/node_modules/contextify/lib/contextify.js:1:101
        at Object.runContentWithLocalBindings (/Users/danvk/code/jest-d3-repro/node_modules/jest-cli/src/lib/utils.js:315:17)
        at Loader._execModule (/Users/danvk/code/jest-d3-repro/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:243:9)
        at Loader.requireModule (/Users/danvk/code/jest-d3-repro/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:886:12)
        at Loader.requireModuleOrMock (/Users/danvk/code/jest-d3-repro/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:907:17)
        at Spec.<anonymous> (/Users/danvk/code/jest-d3-repro/__tests__/simple.js:6:22)
        at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
1 test failed, 0 test passed (1 total)
Run time: 2.225s
@SegFaultx64

This comment has been minimized.

Copy link
Contributor

SegFaultx64 commented Dec 2, 2014

This is caused by jest trying to mock the .node native module in contextify. This fixes it fdecampredon@c108909 . You will then hit Cannot read property 'id' of undefined on line 61 of jsdom. Changing the line immediately above to: if (!options.url && module.parent) { will resolve it, and appears to have no adverse side effects. I am going to submit a pull request to jsdom making that change.

@SegFaultx64

This comment has been minimized.

Copy link
Contributor

SegFaultx64 commented Dec 2, 2014

I submitted that pull request here: jsdom/jsdom#966 . We will see how it goes. Realistically I think the bug exists on the Jest side. module.parent should not me undefined. I am going to look into fixing that on this side but the HastModuleLoader file is monolithic and scary to fiddle with.

@SegFaultx64

This comment has been minimized.

Copy link
Contributor

SegFaultx64 commented Dec 3, 2014

@danvk and @Daniel15: The pull request I made against jsdom (jsdom/jsdom#966) was merged and released as 1.3.2, so as soon as my outstanding pull request against Jest (#192) is merged you should be able to mock jsdom with Jest.

@jeffmo jeffmo closed this in #192 Dec 3, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment