Skip to content

Commit ff3d9b2

Browse files
committed
refactor(@angular/build): extract Vitest CLI option creation from executor
The Vitest CLI level options were assembled inline within the Vitest executor's initialization. They are now created by a dedicated function which allows the mapping from the builder options to be unit tested without starting Vitest. There is no change in behavior.
1 parent d899c51 commit ff3d9b2

3 files changed

Lines changed: 133 additions & 21 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import type { CliOptions } from 'vitest/node';
10+
import type { NormalizedUnitTestBuilderOptions } from '../../options';
11+
12+
/**
13+
* The subset of the normalized builder options that are used to create the Vitest CLI options.
14+
*/
15+
export interface VitestCliOptionsSource extends Pick<
16+
NormalizedUnitTestBuilderOptions,
17+
'workspaceRoot' | 'watch' | 'outputFile' | 'filter' | 'ui' | 'debug'
18+
> {
19+
cacheOptions: Pick<NormalizedUnitTestBuilderOptions['cacheOptions'], 'enabled'>;
20+
}
21+
22+
/**
23+
* Creates the Vitest CLI level options from the normalized builder options.
24+
* These are the options that Vitest would otherwise receive from its own command line.
25+
*
26+
* @param options The normalized builder options to convert.
27+
* @param project The name of the Vitest project to execute.
28+
* @param configPath The path to an external Vitest configuration file, if any.
29+
* @returns The Vitest CLI options.
30+
*/
31+
export function createVitestCliOptions(
32+
options: VitestCliOptionsSource,
33+
project: string,
34+
configPath: string | false | undefined,
35+
): CliOptions {
36+
const { workspaceRoot, watch, outputFile, cacheOptions, filter, ui, debug } = options;
37+
38+
return {
39+
config: configPath,
40+
root: workspaceRoot,
41+
project,
42+
watch,
43+
...(outputFile !== undefined ? { outputFile } : {}),
44+
...(cacheOptions.enabled ? {} : { cache: false as const }),
45+
...(filter !== undefined ? { testNamePattern: filter } : {}),
46+
...(typeof ui === 'boolean' ? { ui } : {}),
47+
...(debug ? { inspectBrk: true, isolate: false, fileParallelism: false } : {}),
48+
};
49+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { type VitestCliOptionsSource, createVitestCliOptions } from './cli-options';
10+
11+
describe('createVitestCliOptions', () => {
12+
const baseOptions: VitestCliOptionsSource = {
13+
workspaceRoot: '/workspace',
14+
watch: false,
15+
outputFile: undefined,
16+
cacheOptions: { enabled: true },
17+
filter: undefined,
18+
ui: undefined,
19+
debug: false,
20+
};
21+
22+
it('should provide the root, project, watch mode, and configuration file', () => {
23+
expect(createVitestCliOptions(baseOptions, 'app', '/workspace/vitest-base.config.mts')).toEqual(
24+
{
25+
config: '/workspace/vitest-base.config.mts',
26+
root: '/workspace',
27+
project: 'app',
28+
watch: false,
29+
},
30+
);
31+
});
32+
33+
it('should disable configuration file lookup when no configuration file is used', () => {
34+
expect(createVitestCliOptions(baseOptions, 'app', false).config).toBeFalse();
35+
});
36+
37+
it('should only include the output file when it is defined', () => {
38+
expect('outputFile' in createVitestCliOptions(baseOptions, 'app', false)).toBeFalse();
39+
expect(
40+
createVitestCliOptions({ ...baseOptions, outputFile: 'results.json' }, 'app', false)
41+
.outputFile,
42+
).toBe('results.json');
43+
});
44+
45+
it('should disable the Vitest cache when the Angular CLI cache is disabled', () => {
46+
expect('cache' in createVitestCliOptions(baseOptions, 'app', false)).toBeFalse();
47+
expect(
48+
createVitestCliOptions({ ...baseOptions, cacheOptions: { enabled: false } }, 'app', false)
49+
.cache,
50+
).toBeFalse();
51+
});
52+
53+
it('should map the filter option to the test name pattern', () => {
54+
expect('testNamePattern' in createVitestCliOptions(baseOptions, 'app', false)).toBeFalse();
55+
expect(
56+
createVitestCliOptions({ ...baseOptions, filter: 'should work' }, 'app', false)
57+
.testNamePattern,
58+
).toBe('should work');
59+
});
60+
61+
it('should only include the UI option when it is a boolean', () => {
62+
expect('ui' in createVitestCliOptions(baseOptions, 'app', false)).toBeFalse();
63+
expect(createVitestCliOptions({ ...baseOptions, ui: false }, 'app', false).ui).toBeFalse();
64+
expect(createVitestCliOptions({ ...baseOptions, ui: true }, 'app', false).ui).toBeTrue();
65+
});
66+
67+
it('should configure the inspector and disable parallelism when debugging', () => {
68+
const options = createVitestCliOptions({ ...baseOptions, debug: true }, 'app', false);
69+
70+
expect(options.inspectBrk).toBeTrue();
71+
expect(options.isolate).toBeFalse();
72+
expect(options.fileParallelism).toBeFalse();
73+
});
74+
75+
it('should not configure the inspector when not debugging', () => {
76+
const options = createVitestCliOptions(baseOptions, 'app', false);
77+
78+
expect('inspectBrk' in options).toBeFalse();
79+
expect('isolate' in options).toBeFalse();
80+
expect('fileParallelism' in options).toBeFalse();
81+
});
82+
});

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

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
import { NormalizedUnitTestBuilderOptions } from '../../options';
3030
import type { TestExecutor } from '../api';
3131
import { setupBrowserConfiguration } from './browser-provider';
32+
import { createVitestCliOptions } from './cli-options';
3233
import { findVitestBaseConfig } from './configuration';
3334
import { createVitestConfigPlugin, createVitestPlugins, rebasePath } from './plugins';
3435

@@ -270,13 +271,11 @@ export class VitestExecutor implements TestExecutor {
270271
const {
271272
coverage,
272273
reporters,
273-
outputFile,
274274
workspaceRoot,
275275
browsers,
276276
debug,
277277
watch,
278278
browserViewport,
279-
ui,
280279
projectRoot,
281280
runnerConfig,
282281
projectSourceRoot,
@@ -346,14 +345,6 @@ export class VitestExecutor implements TestExecutor {
346345
setupFiles: testSetupFiles,
347346
});
348347

349-
const debugOptions = debug
350-
? {
351-
inspectBrk: true,
352-
isolate: false,
353-
fileParallelism: false,
354-
}
355-
: {};
356-
357348
const externalConfigPath =
358349
runnerConfig === true
359350
? await findVitestBaseConfig([projectRoot, workspaceRoot])
@@ -393,17 +384,7 @@ export class VitestExecutor implements TestExecutor {
393384
}
394385
this.debugLog(DebugLogLevel.Verbose, 'Included test files (after filtering):', include);
395386

396-
const vitestConfig = {
397-
config: externalConfigPath,
398-
root: workspaceRoot,
399-
project,
400-
watch,
401-
...(outputFile !== undefined ? { outputFile } : {}),
402-
...(cacheOptions.enabled ? {} : { cache: false as const }),
403-
...(this.options.filter !== undefined ? { testNamePattern: this.options.filter } : {}),
404-
...(typeof ui === 'boolean' ? { ui } : {}),
405-
...debugOptions,
406-
};
387+
const vitestConfig = createVitestCliOptions(this.options, project, externalConfigPath);
407388
const vitestServerConfig = {
408389
// Note `.vitest` is auto appended to the path.
409390
cacheDir: cacheOptions.path,

0 commit comments

Comments
 (0)