Making mocha "use strict" when running in node - javascript

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';

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.

Run tests with js wdio test runner via WebStorm IDE

I'm trying to build a test framework using mocha + webdriver.io.
I've chosen wdio test runner and all tests are running good via CLI but I want to configure WebStorm IDE to run single test in debug mode and can't understand how to do it.
As I understood there is no WebStorm support for this directly and I need to configure default Node.js run with valid parameters in order to trigger wdio runner with my test case.
Check https://alippai.github.io/ for a brief guide. It's running the tests with the mocha executable instead of the default wdio.
The next config is working for me (wdio+jasmine)

How to use Mocha testrunner instead of built-in

I'm creating e2e test using WebdriverIO.
As I understand test-runner calling sequence is smth like
NPM (package.json) -> WDIO (wdio.conf.js) -> Mocha (via "wdio-mocha-framework")
As far as i know, Wdio is designed for using built-in test-runner. But is there way for call wdio from mocha? This is needed for debug in IDE and run test separately.
You can use 'standalone' mode to require the test runner via Mocha.
There are a bunch of examples of standalone mode on their github page.

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.

How to test NodeJS application using Mocha, Grunt?

Maybe someone met any npm module/tutorial/guide/article about "how to test nodejs application using mocha, gruntjs?" Would be glad to any suggestions.
I'd recommend the following (it worked for me):
Install Grunt and get your first sample grunt task running based on the Grunt documentation
Use this grunt task to get server-side Grunt running Mocha tests for you, and this one for client-side tests. From the command line, you can just run $ grunt to watch the results pop up.
At this point, you can check out Mocha interfaces for samples of how your tests can look. Note that there's also should.js, if you're big on rSpec-style tests. Once you've settled on an interface, more Googling/StackOverflowing is the best way to proceed (e.g. Google "Mocha should.js examples").

Categories