diff --git a/packages/vitest/src/node/config/resolveConfig.ts b/packages/vitest/src/node/config/resolveConfig.ts index 1bcba9c9e7a9..21452cd3c05e 100644 --- a/packages/vitest/src/node/config/resolveConfig.ts +++ b/packages/vitest/src/node/config/resolveConfig.ts @@ -623,9 +623,22 @@ export function resolveConfig( ) if (cliReporters.length) { + // When CLI reporters are specified, preserve options from config file + const configReportersMap = new Map>() + + // Build a map of reporter names to their options from the config + for (const reporter of resolved.reporters) { + if (Array.isArray(reporter)) { + const [reporterName, reporterOptions] = reporter + if (typeof reporterName === 'string') { + configReportersMap.set(reporterName, reporterOptions as Record) + } + } + } + resolved.reporters = Array.from(new Set(toArray(cliReporters))) .filter(Boolean) - .map(reporter => [reporter, {}]) + .map(reporter => [reporter, configReportersMap.get(reporter) || {}]) } } diff --git a/test/reporters/fixtures/junit-cli-options/sample.test.ts b/test/reporters/fixtures/junit-cli-options/sample.test.ts new file mode 100644 index 000000000000..dde750fc1f68 --- /dev/null +++ b/test/reporters/fixtures/junit-cli-options/sample.test.ts @@ -0,0 +1,5 @@ +import { test, expect } from 'vitest' + +test('simple test', () => { + expect(1 + 1).toBe(2) +}) diff --git a/test/reporters/fixtures/junit-cli-options/vitest.config.ts b/test/reporters/fixtures/junit-cli-options/vitest.config.ts new file mode 100644 index 000000000000..fce28b45e745 --- /dev/null +++ b/test/reporters/fixtures/junit-cli-options/vitest.config.ts @@ -0,0 +1,12 @@ +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + reporters: [ + ['junit', { + suiteName: 'custom-suite-name', + addFileAttribute: true, + }], + ], + }, +}) diff --git a/test/reporters/tests/junit.test.ts b/test/reporters/tests/junit.test.ts index 6267a48796d0..6a6d0eea35fd 100644 --- a/test/reporters/tests/junit.test.ts +++ b/test/reporters/tests/junit.test.ts @@ -156,3 +156,21 @@ test('many errors without warning', async () => { ) expect(stderr).not.toContain('MaxListenersExceededWarning') }) + +test('CLI reporter option preserves config file options', async () => { + const { stdout } = await runVitestCli( + 'run', + '--reporter=junit', + '--root', + resolve(import.meta.dirname, '../fixtures/junit-cli-options'), + ) + + const xml = stabilizeReport(stdout) + + // Verify that suiteName from config is preserved + expect(xml).not.toContain('