Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 953
user3112401 Avatar asked Aug 31 '14 15:08

user3112401


1 Answers

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.

like image 86
Louis Avatar answered Oct 28 '22 05:10

Louis