简体   繁体   中英

Does Mocha ignore --harmony option in mocha.opts?

In my test directory, I have a file mocha.opts containing the following:

--harmony
--recursive
--growl
--reporter spec
--require should

When I run mocha , I get the following error:

/project/server/utilities/encryption.js:3
const
^^^^^
SyntaxError: Use of const in strict mode.

This is, of course, because my use of const requires ES6 Harmony. When I run mocha --harmony , my tests execute just fine. And the other entries in my mocha.opts file work as expected.

Does the mocha.opts file ignore the --harmony argument for some reason? Or am I doing it wrong? The Mocha docs don't elaborate and I haven't been able to find the answer here or anywhere else.

The asker asks:

When I run mocha --harmony , my tests execute just fine. [...]

Does the mocha.opts file ignore the --harmony argument for some reason?

Yes, mocha.opts ignores the --harmony argument. The --harmony option is not a Mocha option but a Node.js option. This is an option that must be passed to Node.js before it starts executing. However, mocha.opts is read after Node.js has started and so even if Mocha was able to understand the option, it would not be able to do anything about it.

But why does it work on the command line? Shouldn't it be the case that when I run mocha --harmony , Mocha has to first start before parsing the --harmony option? No, because mocha is script that starts the "real" Mocha. The shell script detects --harmony and makes sure it is passed to Node.js when it starts the "real" Mocha.

It's not support as something you can include in mocha.opts . You much add in to the command line when you call mocha . See this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM