Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs mocha es6 modules unexpected token export without babel

I am trying to run with some unit test with mocha on ES6 modules with the following command:

node --experimental-modules .\node_modules\mocha\bin\mocha --reporter progress "client/assets/utils/url-utils.test.mjs"

But I get the following error:

(function (exports, require, module, __filename, __dirname) { export class URLUtils {
                                                              ^^^^^^
SyntaxError: Unexpected token export

Isn't the --experimental-modules param supposed to let me use ES6 modules? I would prefer to avoid using Babel or other transpilers for this.

Node version: 11.7.0

Mocha version: 5.2.0

like image 244
Carlo Avatar asked Nov 07 '22 21:11

Carlo


1 Answers

In order to run mocha with experimental modules, you should:

  1. install mocha-erm: npm install mocha-esm --only-dev
  2. run your tests with npx mocha client/assets/utils/url-utils.test.mjs -r esm
like image 147
vinyll Avatar answered Nov 15 '22 06:11

vinyll