Mocha: Passing Flags to Node.js

Created on 7 Oct 2017  路  17Comments  路  Source: mochajs/mocha

I can't figure out a way to pass the --expose-http2 flag to node. I would like to be able to do:

mocha test.js -- --expose-http2
OR
mocha test.js --node-flags 'expose-http2'

Is this currently possible (using mocha v3.2.0)?

Most helpful comment

The flags Mocha will pass to Node are in bin/mocha (whereas Mocha's own flags are in bin/_mocha). To pass other flags to Node that Mocha does not support, you can node <node flags here> node_modules/mocha/bin/_mocha <mocha arguments here> instead of mocha ....

All 17 comments

Also I need to pass an --experimental-modules flag to Node.js to support the new ES6 (.mjs) modules.

The flags Mocha will pass to Node are in bin/mocha (whereas Mocha's own flags are in bin/_mocha). To pass other flags to Node that Mocha does not support, you can node <node flags here> node_modules/mocha/bin/_mocha <mocha arguments here> instead of mocha ....

Perfect. Thank you @ScottFreeCode 馃憤

@jondubois Did you get it to run with --experimental-modules?

@stefanwalther ScottFreeCode's solution above worked well for me. You just need to run the mocha script with the node command instead of directly through the mocha command. You can add an npm test script to your package.json to use the alternative approach instead of the mocha command.

Well... I cannot get it to work with --experimental-modules because node complains:

$ node --experimental-modules node_modules/.bin/_mocha
Error [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension: node_modules/mocha/bin/_mocha
    at Loader.exports.resolve [as resolver] (internal/loader/ModuleRequest.js:126:13)
    at Loader.resolve (internal/loader/Loader.js:71:40)
    at Loader.getModuleJob (internal/loader/Loader.js:101:40)
    at Loader.import (internal/loader/Loader.js:128:28)

Did you get this to work @michael-brade ?

@seenickcode nope, I gave up on it and won't use modules for the time being. Modules caused a whole lot of problems with the toolchain and I wasted way too much of my precious time on it. Maybe I'll try again in a year or two when it's ready.

you can use esm if you want to use modules. install esm then adding -r esm shoud do the trick.

@pukapukan indeed it does!

@pukapukan works perfectly, thanks!

I was initally confused where to add this (whether it was a node flag or a mocha flag) but my tests are working after the following package.json change:

 "scripts": {
-  "test": "mocha test.js"
+  "test": "mocha test.js -r esm"
 }

hope this helps anyone else slightly confused for a similar reason

@pukapukan works perfectly, thanks!

I was initally confused where to add this (whether it was a node flag or a mocha flag) but my tests are working after the following package.json change:

 "scripts": {
-  "test": "mocha test.js"
+  "test": "mocha test.js -r esm"
 }

hope this helps anyone else slightly confused for a similar reason

ERROR: Cannot find module 'esm'

ERROR: Cannot find module 'esm'

try npm install esm

Has anyone gotten this to work with Node 12 and --experimental-modules?

My test script looks like:

    "test": "node --experimental-modules node_modules/mocha/bin/_mocha \"test/**/*_test.js\"",

But I'm still getting a syntax error on on import {} in my test modules. I have "type": "module" in my package.json too.

@justinfagnani After experimenting with the experimental module support in Node and reading up on how the new support works, I believe testing ES Modules using Mocha alone _cannot be made to work using just Node_ at the moment.

Using the built-in support isn't possible until Mocha decides it to be by changing its core. You can pass the flags to enable module support in Node like this: node --experimental-modules $(npm bin)/_mocha, but it will still crash on running code using import * from './my-module' with the following error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/fatso/dev/sinon/test/es2015/module-support-assessment-test.mjs

Wuuut? The reason Node complains is that Mocha imports the test files using the normal CommonJS require, while Node requires it to use import for it to enable the mode where files are running as ES Modules. That means _Mocha_ must change for this to work. Until that happens, requiring the external esm module (simplest) or using transpilation (if you have no other way) is your answer.

cc: @stefanwalther

mocha -r esm test/*.mjs
node --experimental-modules ./node_modules/mocha/bin/mocha test/*.mjs

both fail for me with Must use import to load ES Module

@brandonros I linked to an article from the Node team on how the new module support works in Node 12. Excerpt:

Files ending in .mjs are explicitly treated as ES modules in import statements and when run via the node command.

That means you _cannot_ expect files ending in .mjs to be treated in any other way, even if loading an additional library: Node will still use a different runtime behaviour for these files. You use the esm library to enable ES Module support when the Node runtime itself _does not support it_, meaning that if your files are not treated as ES Modules by Node itself, the esm module will make sure the syntax does not cause runtime errors when encountering syntax such as import and export.

The only change you need to do is change the file extension to something not explicitly treated by Node in any specific way, like .es6 or just .js. You can see that is what we are doing in Sinon when testing our ESM functionality.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

eschwartz picture eschwartz  路  3Comments

3p3r picture 3p3r  路  3Comments

wzup picture wzup  路  3Comments

Aarbel picture Aarbel  路  3Comments

Swivelgames picture Swivelgames  路  3Comments