I'm trying to run a Mocha test with some ES6 style imports in the file but I keep getting the error:
import assert from 'assert';
^^^^^^
SyntaxError: Unexpected identifier
I tried to invoke mocha with both
mocha --require @babel/register --recursive
and
mocha --require babel-register --recursive
but the error would not go away.
What is the correct way to run ES6 style Mocha tests?
I'm trying to run a Mocha test with some ES6 style imports in the file but I keep getting the error:
import assert from 'assert';
^^^^^^
SyntaxError: Unexpected identifier
I tried to invoke mocha with both
mocha --require @babel/register --recursive
and
mocha --require babel-register --recursive
but the error would not go away.
What is the correct way to run ES6 style Mocha tests?
Share Improve this question asked Jul 12, 2019 at 9:57 user229487user229487 1,40711 silver badges14 bronze badges3 Answers
Reset to default 6For anyone ing from Google:
You can also install esm: npm i esm --save-dev
or use your preferred package manager.
Then pass it as an argument to mocha: mocha 'index.test.js' --require esm
I found the answer to my question here -> https://dev.to/bnorbertjs/my-nodejs-setup-mocha--chai-babel7-es6-43ei
This package.json file
{
"name": "mochatest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha --require @babel/register --recursive"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.1.2",
"@babel/preset-env": "^7.5.4",
"@babel/register": "^7.4.4",
"mocha": "^6.1.4"
},
"dependencies": {}
}
together with this .babelrc
{
"presets": ["@babel/preset-env"]
}
solved my problem.
Try Below Code
import { strict as assert } from 'assert';
Or
import * as assert from 'assert';
Hope this helps