Mocha ".mocharc" Not Loaded - Config Ignored, Wrong Files Run
Mocha reads .mocharc.{js,cjs,json,yml} for spec globs, require hooks, and timeouts. When the file is in the wrong place, in a module format Mocha cannot load, or overridden by CLI flags, your settings silently do not apply.
What this error means
Mocha runs the wrong files (or no files), ignores your require/timeout settings, and behaves as if there is no config. The same flags passed on the CLI work, proving the rc file is being skipped, not the settings themselves.
Error: No test files found: "test"
# .mocharc.yml sets spec: 'src/**/*.spec.ts' and require: ts-node/register
# but Mocha used its default "./test" glob -> rc never loadedCommon causes
Config not in a location Mocha discovers
Mocha looks for .mocharc.* in the cwd (and up the tree). Running from a subdirectory, or placing the file outside the project root, means it is never found.
ESM/CJS rc mismatch or CLI override
A .mocharc.js using module.exports in a "type":"module" package fails to load; and explicit CLI flags take precedence, so a partial command can mask that the rc was ignored.
How to fix it
Use a loadable rc in the right place
Put the rc at the project root and match the module format (use .cjs for CommonJS in an ESM package).
// .mocharc.cjs
module.exports = {
spec: 'src/**/*.spec.ts',
require: 'ts-node/register',
timeout: 10000,
};Confirm Mocha is reading it
- Run Mocha from the project root (or pass
--config <path>explicitly). - Use
.cjs/.json/.ymlto avoid ESM-parsing issues with.jsrc files. - Avoid duplicating settings on the CLI that contradict the rc.
How to prevent it
- Keep
.mocharcat the project root and run from there. - Match the rc module format to the package
"type". - Pass
--configexplicitly in CI to remove ambiguity.