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
Related
When I try to run jest with garbage collection exposed (e.g. as suggested here with node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage) I get errors about cannot load module '#babel/plugin-proposal-nullish-coalescing-operator and a require stack that shows it's looking in my .pnpm directory under node_modules.
Normally I just run 'npm run jest' which my package.json lists as just 'NODE_ENV=test jest'.
We have a jest.config.js that I presume that jest normally just picks up, but if I put -- -c=jest.config.js into the node command above, it looks like it's assuming that's a test file name?
We have a mix of .ts and .js files, as we're moving to TypeScript, and a very long webpack.config.js and babel.conig.js file that I presume does a bunch of work to make modules 'available' to the tests automagically.
So I'm not sure how to proceed from here?
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.
I have project which has code targeting both browser and node. It has .babelrc file for the browser code. But when I'm running tests for the node code using Jest it always reads the .babelrc file which is not required.
So I can somehow disable it?
You need to create an additional jest setting file for your node test. In this file set transform to an empty object. To use this file you need to call jest with the --config option pointing to your node jest setting.
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.
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';