Skip to content
Latchkey

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" --recursive

Configure the spec pattern in .mocharc

.mocharc.json
// .mocharc.json
{
  "spec": "test/**/*.spec.js",
  "recursive": true
}

How to prevent it

  • Commit an explicit spec glob in .mocharc.
  • Use --recursive (or **) for nested test directories.
  • Quote globs so the shell does not expand them unexpectedly.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →