Skip to content

Commit 6ec36f5

Browse files
committed
fix(@angular/build): warn when vitest watch config conflicts with builder
Added a warning in the `angular:vitest-configuration` plugin to alert users if the `watch` option specified in their `vitest.config.ts` differs from the Angular CLI builder's `--watch` option. Because the Angular CLI's build system and file watcher drives the execution, the Vitest-specific `watch` option is overridden and has no effect. (cherry picked from commit ec10eb3)
1 parent 1dd758f commit 6ec36f5

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ export class VitestExecutor implements TestExecutor {
376376
setupFiles: testSetupFiles,
377377
projectPlugins,
378378
include,
379+
watch,
379380
}),
380381
],
381382
};

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ interface VitestConfigPluginOptions {
4646
projectPlugins: Exclude<UserWorkspaceConfig['plugins'], undefined>;
4747
include: string[];
4848
optimizeDepsInclude: string[];
49+
watch: boolean;
4950
}
5051

5152
async function findTestEnvironment(
@@ -97,6 +98,14 @@ export async function createVitestConfigPlugin(
9798
delete testConfig.include;
9899
}
99100

101+
if (testConfig?.watch !== undefined && testConfig.watch !== options.watch) {
102+
this.warn(
103+
`The "test.watch" option in the Vitest configuration file is overridden by the builder's ` +
104+
`watch option. Please use the Angular CLI "--watch" option to enable or disable watch mode.`,
105+
);
106+
delete testConfig.watch;
107+
}
108+
100109
// Merge user-defined plugins from the Vitest config with the CLI's internal plugins.
101110
if (config.plugins) {
102111
const userPlugins = config.plugins.filter(

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,39 @@ describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
290290
// );
291291
});
292292

293+
it('should warn and ignore "test.watch" option from runnerConfig file', async () => {
294+
harness.useTarget('test', {
295+
...BASE_OPTIONS,
296+
watch: false,
297+
runnerConfig: 'vitest.config.ts',
298+
});
299+
300+
harness.writeFile(
301+
'vitest.config.ts',
302+
`
303+
import { defineConfig } from 'vitest/config';
304+
export default defineConfig({
305+
test: {
306+
watch: true,
307+
},
308+
});
309+
`,
310+
);
311+
312+
const { result, logs } = await harness.executeOnce();
313+
expect(result?.success).toBeTrue();
314+
315+
// TODO: Re-enable once Vite logs are remapped through build system
316+
// expect(logs).toContain(
317+
// jasmine.objectContaining({
318+
// level: 'warn',
319+
// message: jasmine.stringMatching(
320+
// 'The "test.watch" option in the Vitest configuration file is overridden by the builder\\'s watch option.',
321+
// ),
322+
// }),
323+
// );
324+
});
325+
293326
it(`should append "test.setupFiles" (string) from runnerConfig to the CLI's setup`, async () => {
294327
harness.useTarget('test', {
295328
...BASE_OPTIONS,

0 commit comments

Comments
 (0)