Skip to content

fix: --reporter-option on CLI overrides values from config (#5532)#5986

Open
devareddy05 wants to merge 1 commit into
mochajs:mainfrom
devareddy05:fix/reporter-options-cli-override
Open

fix: --reporter-option on CLI overrides values from config (#5532)#5986
devareddy05 wants to merge 1 commit into
mochajs:mainfrom
devareddy05:fix/reporter-options-cli-override

Conversation

@devareddy05

Copy link
Copy Markdown

PR Checklist

Overview

--reporter-option (--reporter-options, -O) given on the CLI did not override the same key declared in .mocharc.* or package.json. The config-file value won instead. Regression observed since v8.3.0, still present in v11.7.5+. The maintainer comment on the issue narrowed it to "Mocha not recognizing the clashing options as 'clashing'".

Root cause

In lib/cli/options.mjs, the second call to parse(...) passes CLI args, env, RC, and package.json as configObjects in priority-HIGH→LOW order. yargs-parser's combine-arrays: true concatenates the reporter-option array from every source in that order: [CLI, env, rc, pkg]. The downstream coerce in lib/cli/run.js (around L223-L243) then reduces this array left-to-right with last-writer-wins — so the lowest-priority source ends up taking precedence, which is the inverse of the intended priority order.

Fix

After the second parse, re-emit reporter-option in priority-LOW→HIGH order (pkg, rc, env, CLI). The coerce's existing last-writer-wins reduction now yields the correct result: CLI overrides env overrides rc overrides pkg. Per-key merge semantics are preserved — CLI overrides only the clashing keys; non-clashing keys from the config still flow through.

A small helper (readReporterOption) reads the value under any of the supported source-side aliases (reporter-option, reporter-options, reporterOption, reporterOptions, O) so config files written with either casing/spelling are respected.

Behavior matrix (verified locally)

Scenario Result
CLI output=cli.xml, config output=config.xml CLI wins → cli.xml
CLI output=cli.xml, config output=config.xml, suiteName=FromConfig {output: 'cli.xml', suiteName: 'FromConfig'}
CLI repeats foo=bar foo=baz, no config {foo: 'baz'} (last wins, unchanged)
Env foo=fromEnv, config foo=fromConfig Env wins

Tests

  • New integration test in test/integration/options/reporter-option.spec.js: "should allow the CLI to override clashing keys from the config", driving Mocha via --config <fixture-yaml> + --reporter-option foo=fromCli and asserting the custom reporter prints {"foo":"fromCli","extra":"keepMe"}.
  • New fixture: test/integration/fixtures/options/reporter-option-mocharc.yml.

Validation

  • npm run format:check, npm run tsc — clean
  • npm run test-smoke — 1/1 passing
  • npm run -s test-node-run -- "test/integration/options/reporter-option.spec.js" — 4/4 passing (3 pre-existing + 1 new)
  • npm run -s test-node-run -- "test/integration/options/*.spec.js" — 158/158 passing
  • npm run test-node:unit — 1008 passing, 3 pending

Closes #5532

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: command line reporter-options parameter doesn't override .mocharc.yml

1 participant