Chai test fail with "Unexpected import token"

Hi guys,

I have a project which uses mocha chai unit tests. They work just fine when I run them on the command line (npm run unit).

I try to run them in IntelliJ (by right clicking on the test and selecting run), they fail with the below error.

I have the Node.js plugin installed and I can run tests for the other typescript project just fine. Any ideas?

 

import sinon from 'sinon';
^^^^^^

SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Module.m._compile (/Users/jkramer6/source/www-ui/node_modules/ts-node/src/index.ts:392:23)
at Module._extensions..js (module.js:664:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/jkramer6/source/www-ui/node_modules/ts-node/src/index.ts:395:12)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at /Users/jkramer6/source/www-ui/node_modules/mocha/lib/mocha.js:231:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (/Users/jkramer6/source/www-ui/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/Users/jkramer6/source/www-ui/node_modules/mocha/lib/mocha.js:536:10)
at Object.<anonymous> (/Users/jkramer6/source/www-ui/node_modules/mocha/bin/_mocha:573:18)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3

Process finished with exit code 1

3 comments
Comment actions Permalink

Looks like you are passing .ts file to test runner directly, without pre-processing. Did you try passing --require ts-node/register to Mocha? Please attach a screenshot of your run configuration

2
Comment actions Permalink

In the Mocha configuration dialog, there is a field "Extra Mocha options". As Elena mentioned, I entered --require ts-node/register there and it resolved this issue.

0
Comment actions Permalink

Another solution:
Test template could use outdated node by default. 
In my case, it was node v8.xx, and project required at least v12.xx

0

Please sign in to leave a comment.