Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/vitest/src/node/config/resolveConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,22 @@ export function resolveConfig(
)

if (cliReporters.length) {
// When CLI reporters are specified, preserve options from config file
const configReportersMap = new Map<string, Record<string, unknown>>()

// 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<string, unknown>)
}
}
}

resolved.reporters = Array.from(new Set(toArray(cliReporters)))
.filter(Boolean)
.map(reporter => [reporter, {}])
.map(reporter => [reporter, configReportersMap.get(reporter) || {}])
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/reporters/fixtures/junit-cli-options/sample.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { test, expect } from 'vitest'

test('simple test', () => {
expect(1 + 1).toBe(2)
})
12 changes: 12 additions & 0 deletions test/reporters/fixtures/junit-cli-options/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
reporters: [
['junit', {
suiteName: 'custom-suite-name',
addFileAttribute: true,
}],
],
},
})
18 changes: 18 additions & 0 deletions test/reporters/tests/junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<testsuites name="vitest tests"')
expect(xml).toContain('<testsuites name="custom-suite-name"')

// Verify that addFileAttribute from config is preserved
expect(xml).toContain('file="')
})
Loading