How to specify test directory for mocha?

Unit Testingnode.jsmocha.js

Unit Testing Problem Overview


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

Unit Testing Solutions


Solution 1 - Unit Testing

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.

Solution 2 - Unit Testing

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/

Solution 3 - Unit Testing

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

Solution 4 - Unit Testing

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.

Solution 5 - Unit Testing

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

Solution 6 - Unit Testing

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.

Solution 7 - Unit Testing

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

Solution 8 - Unit Testing

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/

Solution 9 - Unit Testing

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/

Solution 10 - Unit Testing

As mentioned by @superjos in comments use

mocha --recursive "some_dir"

Solution 11 - Unit Testing

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.

Solution 12 - Unit Testing

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

Solution 13 - Unit Testing

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.

Solution 14 - Unit Testing

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

Solution 15 - Unit Testing

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFreewindView Question on Stackoverflow
Solution 1 - Unit TestingBehrangView Answer on Stackoverflow
Solution 2 - Unit TestingJeff DickeyView Answer on Stackoverflow
Solution 3 - Unit Testing250RView Answer on Stackoverflow
Solution 4 - Unit TestingMichael BusheView Answer on Stackoverflow
Solution 5 - Unit TestinggregtzarView Answer on Stackoverflow
Solution 6 - Unit TestingthemefieldView Answer on Stackoverflow
Solution 7 - Unit Testingrajeshchauhan23102008View Answer on Stackoverflow
Solution 8 - Unit TestingAnthony AwuleyView Answer on Stackoverflow
Solution 9 - Unit TestingjfunkView Answer on Stackoverflow
Solution 10 - Unit TestingavckView Answer on Stackoverflow
Solution 11 - Unit TestingRobin LuitenView Answer on Stackoverflow
Solution 12 - Unit TestingCozzbieView Answer on Stackoverflow
Solution 13 - Unit TestingPierreView Answer on Stackoverflow
Solution 14 - Unit Testinglwdthe1View Answer on Stackoverflow
Solution 15 - Unit TestingVitaliy MarkitanovView Answer on Stackoverflow