question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected token import in tests (with Jest on Windows)

See original GitHub issue

Hi guys,

I think the 1.2.4 release has a problem with dynamic-import-node in tests.

A default out of the box project with Jest tests gives the error below (on my Windows computer)

Do you encounter any problems in osx?

> vw-ssr-full@1.0.0 unit C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full
> jest --config test/unit/jest.conf.js --coverage

 FAIL  test\unit\specs\HelloWorld.spec.js
  ● Test suite failed to run

    C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\test\unit\setup.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import Vue from 'vue';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:305:17)
          at Generator.next (<anonymous>)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        6.853s
Ran all test suites.
Running coverage on untested files...
    Failed to collect coverage from C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\src\App.vue
    ERROR: SyntaxError: 'import' and 'export' may only appear at the top level (10:0)
    STACK: SyntaxError: 'import' and 'export' may only appear at the top level (10:0)
    at Parser.pp$5.raise (C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\node_modules\babylon\lib\index.js:4454:13)
    at Parser.pp$1.parseStatement (C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\node_modules\babylon\lib\index.js:1877:16)
    at Parser.parseStatement (C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\node_modules\babylon\lib\index.js:5910:22)
    at Parser.pp$1.parseBlockBody (C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\node_modules\babylon\lib\index.js:2268:21)
    at Parser.pp$1.parseBlock (C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\node_modules\babylon\lib\index.js:2247:8)
    at Parser.pp$3.parseFunctionBody (C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\node_modules\babylon\lib\index.js:4235:22)
    at Parser.parseFunctionBody (C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\node_modules\babylon\lib\index.js:5897:20)
    at Parser.pp$1.parseFunction (C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\node_modules\babylon\lib\index.js:2386:8)
    at Parser.pp$3.parseFunctionExpression (C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\node_modules\babylon\lib\index.js:3760:17)
    at Parser.pp$3.parseExprAtom (C:\Users\kemal\Desktop\Kite\_Code\third-repos\vw-ssr-full\node_modules\babylon\lib\index.js:3722:19)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
LinusBorgcommented, Nov 22, 2017

No, babelrc is fine. The problem is that NODE_ENV is dev.

You see, normally it goes like this:

  1. Jest by defaults sets process.env.NODE_ENV to test, if it’s not already set(!)
  2. if BABEL_ENV is not set, babel falls back to the value of NODE_ENV, so now BABEL_ENV is also dev
  3. As the .babelrc config relies on BABEL_ENV to be test so that the correct env configuration is applied.
  4. If you don’t mess with any of these env variables at all, everything should work just fine, because BABEL_ENV will be test, like we want it to.
  5. but with your setup, it ends up as dev for reasons I don’t know, which means babel doesn’t convert ES6 imports to commonjs.

So long story short:

  • NODE_ENV should be set to test or not set at all, or,
  • if you need NODE_ENV to be dev, then BABEL_ENV should be set to test by you manually.
1reaction
kemalcanycommented, Nov 22, 2017

Thanks for the help Linus, so yes so the BABEL_ENV is undefined. NODE_ENV is dev.

Setting BABEL_ENV as test or production doesn’t seem to solve the issue.

The .babelrc that comes out of the box is as follows:

To me it looks like a Karma setup but at the command prompt I’ve chosen Jest as my test runner, could this be the problem?

{
  "presets": [
    ["env", {
      "modules": false
    }],
    "stage-2"
  ],
  "plugins": ["transform-runtime"],
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": ["transform-es2015-modules-commonjs", "dynamic-import-node"]
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

jest: Test suite failed to run, SyntaxError: Unexpected token ...
Jest sets the env variable to test, so I had to add my presets to the env setting in .babelrc: { "plugins": ["syntax-dynamic-import", ......
Read more >
How I Fixed The Unexpected Token Error In Jest
To put this in different terms, I'm using Jest to test a package with "modern" JavaScript, which in-turn imports another package with ...
Read more >
Configuring Jest
This option tells Jest that all imported modules in your tests should be ... Use the <rootDir> string token to include the path...
Read more >
jest encountered an unexpected token ts-jest - You.com
Test suite failed to run Jest encountered an unexpected token This usually means that you are trying to import a file which Jest...
Read more >
TypeScript Jest: Unexpected Token Export - Reddit
Ok so now your test project is using common JS, but node sees your dependency as an ES Module, so it throws an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found