mochajs/mocha · warning

Warning: ${warning.message}

Error message

Warning: ${warning.message}

What it means

When mocha's file collector finds test files but also spec-file arguments that matched nothing, it treats each unmatched pattern as a warning: for each entry in `unmatchedSpecFiles` it prints `Warning: <message>` to stderr via console.warn. This is not a thrown exception but a diagnostic message indicating some of your file arguments/globs were ignored.

Source

Thrown at lib/cli/collect-files.cjs:106

    ...fileArgs.map((filepath) => path.resolve(filepath)),
    ...specFiles,
  ];
  debug("test files (in order): ", files);

  if (!files.length) {
    // give full message details when only 1 file is missing
    const noneFoundMsg =
      unmatchedSpecFiles.length === 1
        ? `Error: No test files found: ${JSON.stringify(
            unmatchedSpecFiles[0].pattern,
          )}` // stringify to print escaped characters raw
        : "Error: No test files found";
    console.error(pc.red(noneFoundMsg));
    process.exit(1);
  } else {
    // print messages as a warning
    unmatchedSpecFiles.forEach((warning) => {
      console.warn(pc.yellow(`Warning: ${warning.message}`));
    });
  }

  return {
    files,
    unmatchedFiles,
  };
};

View on GitHub (pinned to 6bcbee4fd9)

Solutions

  1. Read each warning's message to identify which pattern matched nothing
  2. Fix or remove the stale path/glob from the CLI args, .mocharc file, or npm script
  3. Verify the file exists relative to the cwd mocha runs in (`ls <pattern>` or expand the glob yourself)
  4. Enable/inspect `unmatchedFiles` in programmatic usage to handle it explicitly

Example fix

// before
"test": "mocha test/unit.spec.js test/does-not-exist.spec.js"
// after
"test": "mocha test/unit.spec.js test/integration.spec.js"
Defensive patterns

Strategy: validation

Validate before calling

const glob = require('glob');
const missing = patterns.filter((p) => glob.sync(p).length === 0);
if (missing.length) console.warn('Patterns matching nothing:', missing);

Type guard

const hasMatches = (pattern) => require('glob').sync(pattern).length > 0;

Try / catch

try {
  const result = collectFiles(args);
  result.unmatchedFiles.forEach((f) => console.warn('No match for:', f));
} catch (err) {
  // only fatal when no files at all were found
  console.error(err.message);
  process.exitCode = 1;
}

Prevention

When it happens

Trigger: Passing `mocha run path/to/spec.js` where the path does not exist or does not match anything, while other valid test files were still found; glob patterns with typos or wrong extensions among otherwise valid args.

Common situations: Renamed or deleted spec files still referenced on the CLI or in .mocharc config; case-sensitivity mismatches on case-sensitive filesystems; globs written for a different directory layout after a repo restructure.

Related errors


AI-assisted analysis of mochajs/mocha@6bcbee4fd9 (2026-09-01). Data as JSON: /api/errors/c2e20852c67d2647. Report an issue: GitHub.