Mocha Recursive Test Execution: How-To
To make Mocha run tests in all subfolders recursively, use the --recursive flag with the spec pattern, e.g., mocha --recursive 'test/**/*.spec.js', or set recursive: true in your .mocharc configuration file. This flag tells Mocha to traverse the directory tree, picking up any test files that match your spec pattern, including those in nested subfolders.
Why Mocha Doesn't Recursively Find Tests by Default
By default, Mocha's test file discovery is shallow. When you run mocha without arguments, it looks for test files in the test directory (or the current directory if no test folder exists) but only at the top level. This is intentional—it keeps test runs fast and predictable, especially in large projects where you might want to run only a subset of tests. However, as projects grow, you often organize tests into subfolders by feature or module, and you want all of them to run in one command.
How the --recursive Flag Works
The --recursive flag changes the behavior of the default test file pattern. Without it, Mocha uses a pattern like test/*.js (or test/*.spec.js if you've configured that). With --recursive, it becomes test/**/*.js, which matches files in all subdirectories. The flag works alongside any custom spec pattern you provide. For example:
mocha --recursiveruns all.jsfiles undertest/recursively.mocha --recursive 'src/**/*.test.js'runs all files matching that pattern, including those in nested folders.mocha --recursive 'test/unit/**/*.spec.js'runs all spec files undertest/unit, recursively.
Note that when you specify a pattern explicitly, the --recursive flag still applies—it essentially adds a globstar (**) to the pattern's directory part. If you omit the flag, Mocha treats the pattern as a literal path, so 'test/**/*.js' would not match files in test/subdir/file.js unless you use the flag or the pattern already includes **.
Setting Recursive Mode in Configuration Files
Instead of typing the flag every time, you can set it in your .mocharc file (JSON, YAML, or JS) or in the mocha property of package.json. Here's how:
- In
.mocharc.json:{ "recursive": true } - In
.mocharc.yml:recursive: true - In
package.json:"mocha": { "recursive": true }
Once set, every mocha invocation will use recursive discovery. This is often the cleanest approach for projects that consistently use nested test folders.
Choosing the Right Spec Pattern
While --recursive handles the directory traversal, you still need a spec pattern that matches your test file naming convention. Common patterns:
*.test.js– matches files likeuser.test.js*.spec.js– matches files likeuser.spec.js*-test.js– matches files likeuser-test.js
If you don't specify a pattern, Mocha defaults to ./test/*.{js,cjs,mjs} (or ./test/**/*.{js,cjs,mjs} with --recursive). For projects where tests live alongside source files (e.g., src/foo/foo.test.js), you'd set a pattern like 'src/**/*.test.js' and combine it with --recursive to ensure nested directories are included.
Common Pitfalls and How to Avoid Them
- Forgetting the flag in CI: If you rely on
npm testwhich callsmochawithout flags, tests in subfolders will be skipped. Always setrecursive: truein your config file to make it consistent across environments. - Glob pattern quoting on Windows: If you pass a pattern on the command line, quote it to prevent shell expansion. On Windows, use double quotes:
mocha --recursive "test/**/*.js". - Accidentally including non-test files: If your pattern is too broad (e.g.,
**/*.js), you might pick up helper files. Use a naming convention like.test.jsor.spec.jsto avoid this. - Node version and glob support: Mocha uses the
globpackage internally, which supports**. However, if you're using a shell that expands globs, the shell might not support**in older versions. Quoting the pattern ensures Mocha handles it, not the shell.
Alternatives to --recursive
If you don't want to use the flag, you can achieve the same effect by specifying a glob pattern that includes ** yourself. For example:
mocha 'test/**/*.js'– this works without--recursivebecause the pattern explicitly includes**.- Use a test runner script that finds files programmatically and passes them to Mocha, but that's more complex and rarely needed.
In practice, using --recursive is simpler and more readable, especially when combined with a config file.
Frequently Asked Questions
Does --recursive work with .mocharc.js?
Yes. In a .mocharc.js file, you can export an object with recursive: true. For example: module.exports = { recursive: true }.
Will --recursive also run tests in node_modules?
No. By default, Mocha excludes node_modules from its file discovery, even with --recursive. You can override this with the --exclude option, but it's rarely a good idea.
Can I use --recursive with --grep to filter tests?
Yes. --recursive controls which files are loaded; --grep filters which tests within those files actually run. They work independently.
What if I have multiple test directories?
You can pass multiple patterns: mocha --recursive 'test/**/*.js' 'spec/**/*.js'. The flag applies to each pattern.
Does --recursive affect performance?
It can, because Mocha will scan more files. But for typical projects, the overhead is negligible. If you have thousands of files, you might want to be more selective with your pattern.