271

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

Cory House
  • 14,235
  • 13
  • 70
  • 87
Freewind
  • 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 Answers15

332

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.

Behrang
  • 9,789
  • 6
  • 24
  • 19
  • 2
    This 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
  • 28
    The 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
  • 11
    In 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
    or just use `--recursive` as I explain below – Jeff Dickey Mar 29 '16 at 18:22
  • 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
    This should be the answer! The double quotes save me – Minh Thai Sep 10 '16 at 02:15
  • 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
155

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/

Maxime Maillet
  • 237
  • 3
  • 15
Jeff Dickey
  • 5,036
  • 4
  • 28
  • 39
  • 12
    This 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
  • 1
    a 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
  • 3
    That'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
  • 2
    you 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
85

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')
250R
  • 35,945
  • 7
  • 33
  • 25
52

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.

Michael Bushe
  • 1,503
  • 16
  • 16
33

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.

themefield
  • 3,847
  • 30
  • 32
29

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
gregtzar
  • 2,458
  • 3
  • 26
  • 31
23

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

20

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/
Anthony Awuley
  • 3,455
  • 30
  • 20
11

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/

jfunk
  • 7,176
  • 4
  • 37
  • 38
4

As mentioned by @superjos in comments use

mocha --recursive "some_dir"

avck
  • 3,535
  • 3
  • 26
  • 38
3

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.

Robin Luiten
  • 5,198
  • 3
  • 20
  • 24
  • 12
    As of today, one can use the `--recursive` option like the following: `mocha --recursive "some_dir"` – superjos Oct 09 '14 at 23:27
  • 3
    Using `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
3

If you are using nodejs, in your package.json under scripts

  1. For global (-g) installations: "test": "mocha server-test" or "test": "mocha server-test/**/*.js" for subdocuments
  2. 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

Cozzbie
  • 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
2

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.

Pierre
  • 6,084
  • 5
  • 32
  • 52
2

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

lwdthe1
  • 1,001
  • 1
  • 16
  • 16
0

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.

Vitaliy Markitanov
  • 2,205
  • 1
  • 24
  • 23