mochajs/mocha · error · TypeError

ERR_MOCHA_INVALID_ARG_VALUE

ERR_MOCHA_INVALID_ARG_VALUE

Error message

invalid reporter option '${opt}'

What it means

Reporter options passed via --reporter-option (-O) must be key=value pairs. coerceReporterOption splits each option on '=' and throws ERR_MOCHA_INVALID_ARG_VALUE if the value doesn't look like 'key=value' (more than one '=' and no proper pairing). This ensures reporter options arrive as a clean object.

Source

Thrown at lib/cli/run.cjs:38

  validateLegacyPlugin,
  runMocha,
} = require("./run-helpers.cjs");
const { ONE_AND_DONES } = require("./one-and-dones.js");
const debug = require("debug")("mocha:cli:run");

exports.command = ["$0 [spec..]", "inspect"];

exports.describe = "Run tests with Mocha";

const camelCase = (name) =>
  name.replace(/-([a-z])/g, (_, char) => char.toUpperCase());

const coerceReporterOption = (opts) =>
  list(opts).reduce((acc, opt) => {
    const pair = opt.split("=");

    if (pair.length > 2 || !pair.length) {
      throw createInvalidArgumentValueError(
        `invalid reporter option '${opt}'`,
        "--reporter-option",
        opt,
        'expected "key=value" format',
      );
    }

    acc[pair[0]] = pair.length === 2 ? pair[1] : true;
    return acc;
  }, {});

const normalizeRunOptions = (argv) => {
  Object.keys(argv).forEach((name) => {
    const reporterOptionMatch = name.match(/^reporter-options?\.(.+)$/);
    if (reporterOptionMatch) {
      argv["reporter-option"] = Object.assign({}, argv["reporter-option"], {
        [reporterOptionMatch[1]]: argv[name],
      });

View on GitHub (pinned to 6bcbee4fd9)

Solutions

  1. Write options as key=value: `--reporter-option output=report.xml`.
  2. For multiple options, repeat the flag (`-O key1=v1 -O key2=v2`) or comma-separate (`-O key1=v1,key2=v2`) if the value contains no '='.
  3. If a value must contain '=', check the specific reporter's docs for its escaping/delimiter support.
  4. Programmatically, pass reporterOptions as an object instead of the string list form to bypass coercion entirely.

Example fix

// before
mocha --reporter mochawesome --reporter-option reportDir:reports
// after
mocha --reporter mochawesome --reporter-option reportDir=reports
Defensive patterns

Strategy: validation

Validate before calling

function assertReporterOption(opt) {
  const pair = opt.split('=');
  if (pair.length !== 2 || !pair[0]) {
    throw new Error(`invalid reporter option '${opt}': expected "key=value" format`);
  }
}
['output=report.xml'].forEach(assertReporterOption);

Type guard

function isKeyValueOption(s) {
  const parts = s.split('=');
  return parts.length === 2 && parts[0].length > 0;
}

Try / catch

try {
  execSync('npx mocha -O ' + opts, {stdio: 'inherit'});
} catch (err) {
  if (String(err.stderr).includes('invalid reporter option')) {
    console.error('Use key=value form for --reporter-option');
  }
}

Prevention

When it happens

Trigger: Running `mocha --reporter xunit --reporter-option output` (no =), `--reporter-option a=b=c` (more than one =), or passing malformed entries in the list form of reporter options programmatically.

Common situations: Passing a JSON blob as one option string; quoting mistakes in shell dropping the '='; reporters that need nested/comma values — users writing key:value instead of key=value; forgetting that repeated flags or comma lists are the supported shapes.

Related errors


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