Mocha tries to find test files under test
by default, how do I specify another dir, e.g. server-test
?
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.
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/
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/
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
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
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.