Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mocha tests with ES6 style imports

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?

like image 863
user229487 Avatar asked Jul 12 '19 09:07

user229487


1 Answers

For anyone coming 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

like image 196
alt146 Avatar answered Sep 16 '22 16:09

alt146