Mocha tries to find test files under test
by default, how do I specify another dir, e.g. server-test
?

- 14,235
- 13
- 70
- 87

- 193,756
- 157
- 432
- 708
-
While not 100% an answer to your question, check out http://jaketrent.com/post/run-single-mocha-test/ - I came here looking for how to run a specific test suite and this shows you can use `.only` and `.skip` to manage which tests you are running. Important during development of a specific feature when you don't want to wait for the whole test suite to run all the time. – Dave Sag Jul 08 '14 at 00:42
15 Answers
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.

- 9,789
- 6
- 24
- 19
-
2This doesn't work for me. If I wish to do it this way I have to do `mocha test/server-test` – jonnie May 01 '15 at 17:08
-
28The double quotes issue had me for ages! Thank you for saving my sanity. Shout out to everyone reading this to pay extra care to those double quotes. – ctrlplusb Oct 08 '15 at 10:18
-
11In my case, I was using `mocha ./**/*.test.js` (so I can colocate the test file with the module file). The pattern stopped working for me when I added a test file at a different level in the file hierarchy than the rest of the tests, and would only find that one odd ball file and not the dozen others that lived on the same level in the hierarchy. Wrapping in quotes fixed it. – Stoutie Nov 09 '15 at 00:23
-
Wow! This should be the accepted answer. My CI tests were passing too easy because I forgot to add the double quotes -- in Windows, all tests get executed even though the pattern is not wrapped in them. – gustavohenke Jan 27 '16 at 04:58
-
1
-
I just got started with the react-starter-kit and they committed the double quotes in the included test and test:watch scripts. I was trying to figure out why it was only running the first *.test.js file it found. This was the problem, thank you! – vim_commando May 10 '16 at 21:02
-
7
-
At the risk of getting banned from stack overflow.. WHAT THE F*#K... I suddenly have 60 failures :D – Karthik T Jun 13 '19 at 10:24
-
Just for an explanation as far as the double quotes: "You should always quote your globs in npm scripts. If you use double quotes, it’s the shell on UNIX that will expand the glob. On the other hand, if you use single quotes, the node-glob module will handle its expansion." - mocha docs – Jordan Jul 02 '22 at 13:24
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/

- 237
- 3
- 15

- 5,036
- 4
- 28
- 39
-
12This is the option with the least friction, and should be the accepted answer IMHO – Nick Tomlin Apr 17 '15 at 20:47
-
i am using one of the boiler plate and it at following line, may be because of windows, can you please check once.node ./node_modules/mocha/bin/mocha $(find api -name '*-test.js') --compilers js:babel-core/register – kobe Aug 17 '16 at 01:41
-
1a gem, also plays nicely with the Webstorm IDE mocha debug configurations, just select "file patterns" and leave the input empty, and it will use mocha.opts – danday74 Jul 28 '17 at 05:43
-
3That's for sure the best answer. Most probably it would be worth to add in your answer also that the `mocha.opts` file can be placed wherever you want to, and then simply execute `mocha` specifying the path to its config file through for example: `mocha --opts ./mocha.opts` – quirimmo Jan 06 '18 at 20:00
-
2you may also just put --recursive in the mocha command line, useful if you define the test script in your package.json – unludo Jan 08 '19 at 08:26
-
`"configuring Mocha via 'mocha.opts' is DEPRECATED and no longer supported."` => So, use `.mocharc.js` instead. https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.js – davidhcefx Aug 30 '23 at 05:30
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')

- 35,945
- 7
- 33
- 25
-
30
-
5So in other words `mocha` has no option to set desired directory structure in config? – Green Jun 20 '17 at 16:37
-
client user$ mocha $(find tests -name '*.js') -bash: mocha: command not found ; Getting this error. – lft93ryt Aug 20 '17 at 02:13
-
1This is too complicated and misleading. Prefer mocha options rather than using an OS command. – Michael Bushe Jun 18 '18 at 05:02
-
The nice way to do this is to add a "test" npm script in package.json that calls mocha with the right arguments. This way your package.json also describes your test structure. It also avoids all these cross-platform issues in the other answers (double vs single quotes, "find", etc.)
To have mocha run all js files in the "test" directory:
"scripts": {
"start": "node ./bin/www", -- not required for tests, just here for context
"test": "mocha test/**/*.js"
},
Then to run only the smoke tests call:
npm test
You can standardize the running of all tests in all projects this way, so when a new developer starts on your project or another, they know "npm test" will run the tests. There is good historical precedence for this (Maven, for example, most old school "make" projects too). It sure helps CI when all projects have the same test command.
Similarly, you might have a subset of faster "smoke" tests that you might want mocha to run:
"scripts": {
"test": "mocha test/**/*.js"
"smoketest": "mocha smoketest/**/*.js"
},
Then to run only the smoke tests call:
npm smoketest
Another common pattern is to place your tests in the same directory as the source that they test, but call the test files *.spec.js. For example: src/foo/foo.js is tested by src/foo/foo.spec.js.
To run all the tests named *.spec.js by convention:
"scripts": {
"test": "mocha **/*.spec.js"
},
Then to run all the tests call:
npm test
See the pattern here? Good. :) Consistency defeats mura.

- 1,503
- 16
- 16
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.

- 3,847
- 30
- 32
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

- 2,458
- 3
- 26
- 31
Now a days(year 2020) you can handle this using mocha configuration file:
Step 1: Create .mocharc.js file at the root location of your application
Step 2: Add below code in mocha config file:
'use strict';
module.exports = {
spec: 'src/app/**/*.test.js'
};
For More option in config file refer this link: https://github.com/mochajs/mocha/blob/master/example/config/.mocharc.js

- 631
- 6
- 8
Run all files in test_directory
including sub directories that match test.js
find ./parent_test_directory -name '*test.js' | xargs mocha -R spec
or use the --recursive
switch
mocha --recursive test_directory/

- 3,455
- 30
- 20
I had this problem just now and solved it by removing the --recursive
option (which I had set) and using the same structure suggested above:
mochify "test/unit/**/*.js"
This ran all tests in all directories under /test/unit/
for me while ignoring the other directories within /test/

- 7,176
- 4
- 37
- 38
I am on Windows 7 using node.js v0.10.0 and mocha v1.8.2 and npm v1.2.14. I was just trying to get mocha to use the path test/unit to find my tests, After spending to long and trying several things I landed,
Using the "test/unit/*.js" option does not work on windows. For good reasons that windows shell doesn't expand wildcards like unixen.
However using "test/unit" does work, without the file pattern. eg. "mocha test/unit" runs all files found in test/unit folder.
This only still runs one folder files as tests but you can pass multiple directory names as parameters.
Also to run a single test file you can specify the full path and filename. eg. "mocha test/unit/mytest1.js"
I actually setup in package.json for npm "scripts": { "test": "mocha test/unit" },
So that 'npm test' runs my unit tests.

- 5,198
- 3
- 20
- 24
-
12As of today, one can use the `--recursive` option like the following: `mocha --recursive "some_dir"` – superjos Oct 09 '14 at 23:27
-
3Using `node_modules\.bin\mocha "test\unit\*.js"` works on Windows. Also `node_modules\.bin\mocha "**\*.js"` works (my real case). But I'm looking for a way to exclude the _node_modules_ directory. (I use gulpfile.js too but sometime I need to launch test directly with mocha) – Alex 75 Dec 14 '15 at 23:39
If you are using nodejs
, in your package.json
under scripts
- For
global (-g)
installations:"test": "mocha server-test"
or"test": "mocha server-test/**/*.js"
for subdocuments - For
project
installations:"test": "node_modules/mocha/bin/mocha server-test"
or"test": "node_modules/mocha/bin/mocha server-test/**/*.js"
for subdocuments
Then just run your tests normally as npm test

- 1,014
- 2
- 12
- 26
-
I'm finding that `npm run mocha "./test/*.spec.js!(~)"` ignores the glob argument, but `node_modules/.bin/mocha "./test/*.spec.js!(~)"` doesn't. – ironchicken Jan 02 '18 at 13:42
-
@ironchicken to pass arguments to from "npm run" use -- as in `npm run mocha -- yourArgs` – Pedro A Feb 02 '18 at 16:51
This doesn't seem to be any "easy" support for changing test directory.
However, maybe you should take a look at this issue, relative to your question.

- 6,084
- 5
- 32
- 52
As @jeff-dickey suggested, in the root of your project, make a folder called test
. In that folder, make a file called mocha.opts
. Now where I try to improve on Jeff's answer, what worked for me was instead of specifying the name of just one test folder, I specified a pattern to find all tests to run in my project by adding this line:
*/tests/*.js --recursive
in mocha.opts
If you instead want to specify the exact folders to look for tests in, I did something like this:
shared/tests/*.js --recursive
server/tests/graph/*.js --recursive
I hope this helps anyone who needed more than what the other answers provide

- 1,001
- 1
- 16
- 16
Another option in windows could be using cross-env
package and following npm script in package.json
"scripts": {
"test": "cross-env mocha '*.test.js'"
},
"devDependencies": {
"cross-env": "latest",
}
In this case all test files with pattern *.test.js in root folder will be run by mocha.

- 2,205
- 1
- 24
- 23