Skip to content

Commit 58bcba8

Browse files
committed
fix(@angular/build): prevent reporter duplicates by explicitly overriding Vitest configuration
When leveraging the Angular CLI to run tests with a specific set of `--reporters`, Vitest's underlying `mergeConfig` logic would normally array-concatenate the CLI reporters with any reporters defined natively inside `vitest.config.ts`. This led to duplicate output processors (e.g. running 'default' twice). By explicitly wiping the original configurations when CLI overrides are present, the CLI now acts as a strict Source-of-Truth array replacer, which is the expected behavior for CI and custom targets.
1 parent 70fb546 commit 58bcba8

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ export async function createVitestConfigPlugin(
8282
async config(config) {
8383
const testConfig = config.test;
8484

85+
if (reporters !== undefined) {
86+
delete testConfig?.reporters;
87+
}
88+
89+
if (
90+
options.coverage.reporters !== undefined &&
91+
testConfig?.coverage &&
92+
'reporter' in testConfig.coverage
93+
) {
94+
delete testConfig.coverage.reporter;
95+
}
96+
8597
if (testConfig?.projects?.length) {
8698
this.warn(
8799
'The "test.projects" option in the Vitest configuration file is not supported. ' +

packages/angular/build/src/builders/unit-test/tests/behavior/runner-config-vitest_spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
4242
harness.expectFile('vitest-results.xml').toExist();
4343
});
4444

45+
it('should override reporters defined in runnerConfig file when CLI option is present', async () => {
46+
harness.useTarget('test', {
47+
...BASE_OPTIONS,
48+
runnerConfig: 'vitest.config.ts',
49+
reporters: ['default'],
50+
});
51+
52+
harness.writeFile('vitest.config.ts', VITEST_CONFIG_CONTENT);
53+
54+
const { result } = await harness.executeOnce();
55+
expect(result?.success).toBeTrue();
56+
// The CLI option 'default' should override the 'junit' reporter in VITEST_CONFIG_CONTENT
57+
harness.expectFile('vitest-results.xml').toNotExist();
58+
});
59+
4560
it('should use custom reportsDirectory defined in runnerConfig file', async () => {
4661
harness.useTarget('test', {
4762
...BASE_OPTIONS,

0 commit comments

Comments
 (0)