Skip to content

Commit a9b12f1

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 d3295e1 commit a9b12f1

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 } from './plugins';
3435

@@ -266,13 +267,11 @@ export class VitestExecutor implements TestExecutor {
266267
const {
267268
coverage,
268269
reporters,
269-
outputFile,
270270
workspaceRoot,
271271
browsers,
272272
debug,
273273
watch,
274274
browserViewport,
275-
ui,
276275
projectRoot,
277276
runnerConfig,
278277
projectSourceRoot,
@@ -342,14 +341,6 @@ export class VitestExecutor implements TestExecutor {
342341
setupFiles: testSetupFiles,
343342
});
344343

345-
const debugOptions = debug
346-
? {
347-
inspectBrk: true,
348-
isolate: false,
349-
fileParallelism: false,
350-
}
351-
: {};
352-
353344
const externalConfigPath =
354345
runnerConfig === true
355346
? await findVitestBaseConfig([projectRoot, workspaceRoot])
@@ -389,17 +380,7 @@ export class VitestExecutor implements TestExecutor {
389380
}
390381
this.debugLog(DebugLogLevel.Verbose, 'Included test files (after filtering):', include);
391382

392-
const vitestConfig = {
393-
config: externalConfigPath,
394-
root: workspaceRoot,
395-
project,
396-
watch,
397-
...(outputFile !== undefined ? { outputFile } : {}),
398-
...(cacheOptions.enabled ? {} : { cache: false as const }),
399-
...(this.options.filter !== undefined ? { testNamePattern: this.options.filter } : {}),
400-
...(typeof ui === 'boolean' ? { ui } : {}),
401-
...debugOptions,
402-
};
383+
const vitestConfig = createVitestCliOptions(this.options, project, externalConfigPath);
403384
const vitestServerConfig = {
404385
// Note `.vitest` is auto appended to the path.
405386
cacheDir: cacheOptions.path,

0 commit comments

Comments
 (0)