Skip to content
Latchkey

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.

Mocha output
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 loaded

Common 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
// .mocharc.cjs
module.exports = {
  spec: 'src/**/*.spec.ts',
  require: 'ts-node/register',
  timeout: 10000,
};

Confirm Mocha is reading it

  1. Run Mocha from the project root (or pass --config <path> explicitly).
  2. Use .cjs/.json/.yml to avoid ESM-parsing issues with .js rc files.
  3. Avoid duplicating settings on the CLI that contradict the rc.

How to prevent it

  • Keep .mocharc at the project root and run from there.
  • Match the rc module format to the package "type".
  • Pass --config explicitly in CI to remove ambiguity.

Frequently asked questions

What causes ".mocharc not applied"?
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.
How do I fix .mocharc not applied?
Put the rc at the project root and match the module format (use .cjs for CommonJS in an ESM package).

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card