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

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.

Related

How does Jest (or Mocha) work without having to be imported?

Almost every module that we use in NodeJS, need to be imported in some way. These modules offer functions that we can use. However, I have noticed that testing frameworks like mocha and jest dont seem to work the same way. You just include "mocha" or "jest" under the "test" script in package.json, and it does all the work.
I am very curious as to how this works. How can we make a script / function execute just by mentioning a keyword under "scripts" in package.json.
Would be very helpful if someone can answer this! :)
The keyword mocha is actually a CLI, just like ls and cat if you use linux.
If you are asking for how to build a CLI with Node.js, the following references might help.
Building command line tools with Node.js
How to build a CLI with Node.js
How mocha works
By default, mocha looks for the glob "./test/*.js", so you may want to put your tests in test/ folder. If you want to include subdirectories, pass the --recursive option.
Reference: https://mochajs.org/#the-test-directory
Run the mocha CLI.
The program sets the global variables (e.g. describe, it, etc.).
The program loads all javascript files under the test directory.
We are running tests by executing "npm run test" or "npm test" command. Not by executing "node test.js", "npm start" or "npm run start". Calling test command executes the test lib CLI, mocha, jest, etc. This means that you are sending your test.js files to the test CLI as arguments. As a result your test commands (it, describe, etc) interpreted by test CLI, not the javascript/node. If you try "node test.js" you'll get a "ReferenceError: it/describe is not defined".
Shortly, since your test.js files executed by test CLI (mocha, jest, etc) you do not need to import these libs.

What does -c flag do in NodeJS?

In my MacOS terminal, I typed node -c (the -c flag was by mistake). But it worked exactly like node and I can't see any difference.
I searched online but I am unable to find any reference to the -c flag.
Does anyone know what it does??
The -c flag usually checks the syntax without having your node.js script executed.
When you type, node --help you will see the usage listed like this,
-c, --check syntax check script without executing
Hope this helps!
If you type node --help you can get all available options.
-c, --check syntax check script without executing
According to the documentation:
-c, --check
Check the script's syntax without executing it. Exits with an error code if script is invalid.

does jest have a --require command like mocha

Does Jest have a similar option like mocha's --require? I would prefer to have an option like --require in the command line rather than putting a require(x) in the title of every file.
Jest configuration provides setupFiles and setupTestFrameworkScriptFile which can be used to run setup code before tests run.
The require(x) statement would need to perform all of the needed side-effects, there isn't a way to access the module exports of x from a require statement in the setup file, but it looks like that is exactly how the --require option works in Mocha.
Just create a setup file that calls require(x) and either add it to the Jest config using one of the two options listed above or pass it on the command line using the --setupTestFrameworkScriptFile option

JavaScript: How to use WebStorm debugger with ava

This is not question, just answer:
Create run/debug configuration, type Node.js
Select your node interpreter
As node parameter insert your ava bin and parameter --verbose
For me it's: ./node_modules/.bin/ava --verbose
Select your working directory
Done, now you can debug
The magic is in --verbose, I have no idea why it works that way, but it does.
I have no idea why/how it works for you - configuration is definitely wrong.
With your configuration, --inspect-brk is passed to ava, not to Node.js, and thus treated as your application argument. You should have specified node_modules/.bin/ava as JavaScript file: in your Run configuration instead of specifying it as a Node parameter, to make sure that Node debug arguments are passed before the application main file. --verbose can be passed as application parameter.
See also https://github.com/avajs/ava/blob/master/docs/recipes/debugging-with-webstorm.md
Create a new node test runner with following configuration:
Node interpreter: whatever version of node that you are using that is compatible with your ava version
Working directory: ~/Documents/Work/projectRootDir
Javascript file: node_modules/ava/cli.js
Application parameters: -v outdir/testFile e.g. /dist/test/controllers/test.js
There you go, now you can run and debug AVA with the best javascript IDE instead of console logging!
I'm quite sure that vscode config will be quite similar

Making mocha "use strict" when running in node

Is there a way to make Mocha run tests in strict mode when running on node?
Normally you can enable this in node by running node --use_strict. Is there a way to do the same thing for mocha?
Add --use_strict to the mocha command.
So your command might look like this :
mocha ./test --recursive --use_strict
There is no way to pass --use_strict into mocha and this was done in purpose. Read the discussion for details.
If you really want this behaviour you could fork mocha's sources or load this npm module as a first thing in your tests.
I would just start test scripts with
'use strict';

Categories