fix: --reporter-option on CLI overrides values from config (#5532)#5986
Open
devareddy05 wants to merge 1 commit into
Open
fix: --reporter-option on CLI overrides values from config (#5532)#5986devareddy05 wants to merge 1 commit into
devareddy05 wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
status: accepting prsOverview
--reporter-option(--reporter-options,-O) given on the CLI did not override the same key declared in.mocharc.*orpackage.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 toparse(...)passes CLI args, env, RC, andpackage.jsonasconfigObjectsin priority-HIGH→LOW order.yargs-parser'scombine-arrays: trueconcatenates thereporter-optionarray from every source in that order:[CLI, env, rc, pkg]. The downstream coerce inlib/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-optionin 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)
output=cli.xml, configoutput=config.xmlcli.xmloutput=cli.xml, configoutput=config.xml,suiteName=FromConfig{output: 'cli.xml', suiteName: 'FromConfig'}foo=bar foo=baz, no config{foo: 'baz'}(last wins, unchanged)foo=fromEnv, configfoo=fromConfigTests
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=fromCliand asserting the custom reporter prints{"foo":"fromCli","extra":"keepMe"}.test/integration/fixtures/options/reporter-option-mocharc.yml.Validation
npm run format:check,npm run tsc— cleannpm run test-smoke— 1/1 passingnpm 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 passingnpm run test-node:unit— 1008 passing, 3 pendingCloses #5532