Mocha tries to find test files under test
by default, how do I specify another dir, e.g. server-test
?
If in node.js, some new configurations as of Mocha v6:
Option 1: Create .mocharc.json
in project's root directory:
{
"spec": "path/to/test/files"
}
Option 2: add mocha
property in project's package.json
:
{
...
"mocha": {
"spec": "path/to/test/files"
}
}
More options are here.
If you are using nodejs
, in your package.json
under scripts
global (-g)
installations: "test": "mocha server-test"
or "test": "mocha server-test/**/*.js"
for subdocumentsproject
installations: "test": "node_modules/mocha/bin/mocha server-test"
or "test": "node_modules/mocha/bin/mocha server-test/**/*.js"
for subdocumentsThen just run your tests normally as npm test
Edit : This option is deprecated : https://mochajs.org/#mochaopts
If you want to do it by still just running mocha
on the command line, but wanted to run the tests in a folder ./server-tests
instead of ./test
, create a file at ./test/mocha.opts
with just this in the file:
server-tests
If you wanted to run everything in that folder and subdirectories, put this into test/mocha.opts
server-tests
--recursive
mocha.opts
are the arguments passed in via the command line, so making the first line just the directory you want to change the tests too will redirect from ./test/
Here's one way, if you have subfolders in your test folder e.g.
/test
/test/server-test
/test/other-test
Then in linux you can use the find command to list all *.js files recursively and pass it to mocha:
mocha $(find test -name '*.js')
Don't use the -g or --grep option, that pattern operates on the name of the test inside of it(), not the filesystem. The current documentation is misleading and/or outright wrong concerning this. To limit the entire command to a portion of the filesystem, you can pass a pattern as the last argument (its not a flag).
For example, this command will set your reporter to spec but will only test js files immediately inside of the server-test directory:
mocha --reporter spec server-test/*.js
This command will do the same as above, plus it will only run the test cases where the it() string/definition of a test begins with "Fnord:":
mocha --reporter spec --grep "Fnord:" server-test/*.js
Use this:
mocha server-test
Or if you have subdirectories use this:
mocha "server-test/**/*.js"
Note the use of double quotes. If you omit them you may not be able to run tests in subdirectories.