"SyntaxError: Cannot use import statement outside a module"
I have tried to follow the steps in README.md under Step by step guide, but run into the error below. (I am new to Node.js)
$ npx mocha test/**/*.test.{js,ts}
Warning: Cannot find any files matching pattern "test/**/*.test.js"
/root/Waffle/examples/basic/test/BasicToken.test.ts:1
import {expect, use} from 'chai';
^^^^^^
SyntaxError: Cannot use import statement outside a module
I specify the test file format because I hit snags on:
$ npx mocha
Error: No test files found
and
npx mocha --config test/mocha.opts
failed to parse config "test/mocha.opts": SyntaxError: Unexpected token r in JSON at position 1
It would be great if the README was improved as a result of this issue.
I followed the getting started instructions and have exactly the same error message, though using a slightly different command to run the test:
npm test test/BasicToken.test.ts
I'm using node v14.4.0 on Ubuntu 20.04.1 LTS.
I believe this command should work instead:
npx mocha -r ts-node/register --timeout 5000 test/*.test.ts.
You should add ts-node to devDependencies
Ok I have the same two problems: no test file found and cannot use import outside of module.
You can fix the first with specifying the ./test/** or whichever folder you placed the tests.
I knocked the second error out by adding tsc to the "test" line of package.json:
"test": "set NODE_ENV=test && tsc && mocha ./test/**.ts"