Skip to content

Commit c4719c6

Browse files
committed
refactor: replace string literals with TestRunner enum for test runner options.
Replace string with enum (cherry picked from commit cc66824)
1 parent 2ffdae4 commit c4719c6

File tree

7 files changed

+19
-19
lines changed

7 files changed

+19
-19
lines changed

packages/angular/build/src/builders/unit-test/options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import path from 'node:path';
1212
import { normalizeCacheOptions } from '../../utils/normalize-cache';
1313
import { getProjectRootPaths } from '../../utils/project-metadata';
1414
import { isTTY } from '../../utils/tty';
15-
import type { Schema as UnitTestBuilderOptions } from './schema';
15+
import { Runner, type Schema as UnitTestBuilderOptions } from './schema';
1616

1717
export type NormalizedUnitTestBuilderOptions = Awaited<ReturnType<typeof normalizeOptions>>;
1818

@@ -56,7 +56,7 @@ export async function normalizeOptions(
5656

5757
const { runner, browsers, progress, filter, browserViewport, ui, runnerConfig } = options;
5858

59-
if (ui && runner !== 'vitest') {
59+
if (ui && runner !== Runner.Vitest) {
6060
throw new Error('The "ui" option is only available for the "vitest" runner.');
6161
}
6262

@@ -95,7 +95,7 @@ export async function normalizeOptions(
9595
include: options.include ?? ['**/*.spec.ts'],
9696
exclude: options.exclude,
9797
filter,
98-
runnerName: runner ?? 'vitest',
98+
runnerName: runner ?? Runner.Vitest,
9999
coverage: {
100100
enabled: options.coverage,
101101
exclude: options.coverageExclude,

packages/angular/build/src/builders/unit-test/tests/setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
ApplicationBuilderOptions as ApplicationSchema,
1515
buildApplication,
1616
} from '../../../builders/application';
17-
import { Schema } from '../schema';
17+
import { Runner, Schema } from '../schema';
1818

1919
// TODO: Consider using package.json imports field instead of relative path
2020
// after the switch to rules_js.
@@ -61,7 +61,7 @@ export const UNIT_TEST_BUILDER_INFO = Object.freeze({
6161
export const BASE_OPTIONS = Object.freeze<Schema>({
6262
buildTarget: 'test:build',
6363
tsConfig: 'src/tsconfig.spec.json',
64-
runner: 'vitest' as any,
64+
runner: Runner.Vitest,
6565
});
6666

6767
/**

packages/schematics/angular/application/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { latestVersions } from '../utility/latest-versions';
3535
import { relativePathToWorkspaceRoot } from '../utility/paths';
3636
import { getWorkspace, updateWorkspace } from '../utility/workspace';
3737
import { Builders, ProjectType } from '../utility/workspace-models';
38-
import { Schema as ApplicationOptions, Style } from './schema';
38+
import { Schema as ApplicationOptions, Style, TestRunner } from './schema';
3939

4040
const APPLICATION_DEV_DEPENDENCIES = [
4141
{ name: '@angular/compiler-cli', version: latestVersions.Angular },
@@ -341,7 +341,7 @@ function addAppToWorkspaceFile(options: ApplicationOptions, appDir: string): Rul
341341
: {
342342
builder: Builders.BuildUnitTest,
343343
options:
344-
options.testRunner === 'vitest'
344+
options.testRunner === TestRunner.Vitest
345345
? {}
346346
: {
347347
runner: 'karma',

packages/schematics/angular/application/index_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/te
1010
import { parse as parseJson } from 'jsonc-parser';
1111
import { latestVersions } from '../utility/latest-versions';
1212
import { Schema as WorkspaceOptions } from '../workspace/schema';
13-
import { Schema as ApplicationOptions, Style, ViewEncapsulation } from './schema';
13+
import { Schema as ApplicationOptions, Style, TestRunner, ViewEncapsulation } from './schema';
1414

1515
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1616
function readJsonFile(tree: UnitTestTree, path: string): any {
@@ -442,7 +442,7 @@ describe('Application Schematic', () => {
442442
});
443443

444444
it('should set values in angular.json correctly when testRunner is karma', async () => {
445-
const options = { ...defaultOptions, projectRoot: '', testRunner: 'karma' as const };
445+
const options = { ...defaultOptions, projectRoot: '', testRunner: TestRunner.Karma };
446446
const tree = await schematicRunner.runSchematic('application', options, workspaceTree);
447447

448448
const config = JSON.parse(tree.readContent('/angular.json'));

packages/schematics/angular/library/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { latestVersions } from '../utility/latest-versions';
3333
import { relativePathToWorkspaceRoot } from '../utility/paths';
3434
import { getWorkspace, updateWorkspace } from '../utility/workspace';
3535
import { Builders, ProjectType } from '../utility/workspace-models';
36-
import { Schema as LibraryOptions } from './schema';
36+
import { Schema as LibraryOptions, TestRunner } from './schema';
3737

3838
const LIBRARY_DEV_DEPENDENCIES = [
3939
{ name: '@angular/compiler-cli', version: latestVersions.Angular },
@@ -115,7 +115,7 @@ function addLibToWorkspaceFile(
115115
},
116116
},
117117
test:
118-
options.testRunner === 'vitest'
118+
options.testRunner === TestRunner.Vitest
119119
? {
120120
builder: Builders.BuildUnitTest,
121121
options: {
@@ -173,7 +173,7 @@ export default function (options: LibraryOptions): Rule {
173173
angularLatestVersion: latestVersions.Angular.replace(/~|\^/, ''),
174174
tsLibLatestVersion: latestVersions['tslib'].replace(/~|\^/, ''),
175175
folderName,
176-
testTypesPackage: options.testRunner === 'vitest' ? 'vitest/globals' : 'jasmine',
176+
testTypesPackage: options.testRunner === TestRunner.Vitest ? 'vitest/globals' : 'jasmine',
177177
}),
178178
move(libDir),
179179
]);

packages/schematics/angular/ng-new/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import { Schema as ApplicationOptions } from '../application/schema';
2626
import { JSONFile } from '../utility/json-file';
2727
import { Schema as WorkspaceOptions } from '../workspace/schema';
28-
import { Schema as NgNewOptions } from './schema';
28+
import { Schema as NgNewOptions, TestRunner } from './schema';
2929

3030
export default function (options: NgNewOptions): Rule {
3131
if (!options.directory) {
@@ -68,12 +68,12 @@ export default function (options: NgNewOptions): Rule {
6868
apply(empty(), [
6969
schematic('workspace', workspaceOptions),
7070
(tree: Tree) => {
71-
if (options.testRunner === 'karma') {
71+
if (options.testRunner === TestRunner.Karma) {
7272
const file = new JSONFile(tree, 'angular.json');
7373
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7474
const schematics = file.get(['schematics']) ?? ({} as any);
75-
(schematics['@schematics/angular:application'] ??= {}).testRunner = 'karma';
76-
(schematics['@schematics/angular:library'] ??= {}).testRunner = 'karma';
75+
(schematics['@schematics/angular:application'] ??= {}).testRunner = TestRunner.Karma;
76+
(schematics['@schematics/angular:library'] ??= {}).testRunner = TestRunner.Karma;
7777

7878
file.modify(['schematics'], schematics);
7979
}

packages/schematics/angular/ng-new/index_spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
10-
import { Schema as NgNewOptions } from './schema';
10+
import { Schema as NgNewOptions, TestRunner } from './schema';
1111

1212
describe('Ng New Schematic', () => {
1313
const schematicRunner = new SchematicTestRunner(
@@ -159,7 +159,7 @@ describe('Ng New Schematic', () => {
159159
});
160160

161161
it(`should set 'testRunner' to 'karma'`, async () => {
162-
const options = { ...defaultOptions, testRunner: 'karma' as const };
162+
const options = { ...defaultOptions, testRunner: TestRunner.Karma };
163163
const tree = await schematicRunner.runSchematic('ng-new', options);
164164

165165
const {
@@ -178,7 +178,7 @@ describe('Ng New Schematic', () => {
178178
});
179179

180180
it(`should set 'testRunner' to 'karma' in workspace schematic options`, async () => {
181-
const options = { ...defaultOptions, testRunner: 'karma' as const };
181+
const options = { ...defaultOptions, testRunner: TestRunner.Karma };
182182
const tree = await schematicRunner.runSchematic('ng-new', options);
183183

184184
const { schematics } = JSON.parse(tree.readContent('/bar/angular.json'));

0 commit comments

Comments
 (0)