Skip to content

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

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

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

@devareddy05
devareddy05 force-pushed the fix/reporter-options-cli-override branch from 275a63d to 2e71d62 Compare June 11, 2026 16:07
@codecov

codecov Bot commented Jun 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.92%. Comparing base (87696d5) to head (2e71d62).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
lib/cli/options.mjs 0.00% 12 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5986      +/-   ##
==========================================
- Coverage   78.12%   77.92%   -0.21%     
==========================================
  Files          63       63              
  Lines        4590     4602      +12     
  Branches      997      984      -13     
==========================================
  Hits         3586     3586              
- Misses       1004     1016      +12     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mark-wiemer

Copy link
Copy Markdown
Member

Test failure is known issue #6036

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! And sorry it took so long to review. But:

For just one of those I'd request changes, but for both I'm going to go ahead and close this out. Feel free to send another PR if you'd like though - I left a couple of thoughts on this one. Cheers!

Comment thread lib/cli/options.mjs
Comment on lines 283 to +288
* @alias module:lib/cli.loadOptions
* @returns {external:yargsParser.Arguments} Parsed args from everything
*/
// Reads `reporter-option` from a config source under any of its known
// aliases. Keeps `loadOptions` agnostic of how each source was authored.
const readReporterOption = (source) =>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Comments] This // comment conflicts with the existing JSDoc for the function. This should add to the JSDoc instead.

Comment thread lib/cli/options.mjs
].reduce((acc, src) => (src ? acc.concat(list(src)) : acc), []);
if (orderedReporterOption.length) {
args["reporter-option"] = orderedReporterOption;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Refactor] This is quite a lot of complexity for a pretty small area of bug. And, we actually refactored away yargs & yargs-parser on main in the meantime. The mergeConfigObjects we added on main would be a good place for this kind of thing.

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

3 participants