Unexpected token import in tests (with Jest on Windows)
See original GitHub issueHi 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:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
No,
babelrc
is fine. The problem is thatNODE_ENV
isdev
.You see, normally it goes like this:
process.env.NODE_ENV
totest
, if it’s not already set(!)BABEL_ENV
is not set, babel falls back to the value ofNODE_ENV
, so nowBABEL_ENV
is alsodev
.babelrc
config relies onBABEL_ENV
to betest
so that the correct env configuration is applied.test
, like we want it to.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 totest
or not set at all, or,NODE_ENV
to bedev
, thenBABEL_ENV
should be set totest
by you manually.Thanks for the help Linus, so yes so the BABEL_ENV is undefined. NODE_ENV is dev.
Setting BABEL_ENV as
test
orproduction
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?