Mocha "Error: No test files found" in CI
Mocha was given a spec pattern (or its default ./test) that matched no files, so it errors out. Usually the glob is wrong, tests live in a different directory, or --recursive is missing for nested specs.
What this error means
Mocha exits with "Error: No test files found" and a non-zero code, failing the CI job. Running a single file by path works, hiding that the configured glob matches nothing.
mocha
Error: No test files found
at Object.<anonymous> (node_modules/mocha/lib/cli/run-helpers.js)Common causes
Spec glob matches nothing
The pattern passed to Mocha (or in .mocharc) does not match your filenames or directory layout, so no files are collected.
Nested specs without --recursive
Mocha does not descend into subdirectories by default. Tests under nested folders are missed unless --recursive or a ** glob is used.
How to fix it
Point Mocha at the right files
Terminal
# explicit recursive glob
mocha "test/**/*.spec.js" --recursiveConfigure the spec pattern in .mocharc
.mocharc.json
// .mocharc.json
{
"spec": "test/**/*.spec.js",
"recursive": true
}How to prevent it
- Commit an explicit
specglob in.mocharc. - Use
--recursive(or**) for nested test directories. - Quote globs so the shell does not expand them unexpectedly.
Related guides
Mocha "Timeout of 2000ms exceeded ... ensure done() is called"Fix Mocha "Error: Timeout of 2000ms exceeded. For async tests... ensure done() is called" in CI - a missing d…
Mocha "Cannot find module 'ts-node/register'" in CIFix Mocha "Error: Cannot find module 'ts-node/register'" in CI - ts-node not installed, or required before it…
Vitest "No test files found, exiting with code 1" in CIFix Vitest "No test files found, exiting with code 1" in CI - an include glob that matches nothing, the wrong…