Skip to content

Commit 21ef181

Browse files
committed
refactor: replace string literals with TestRunner enum for test runner options.
Replace string with enum
1 parent a2923f2 commit 21ef181

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
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: 3 additions & 3 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 },
@@ -189,7 +189,7 @@ function addDependenciesToPackageJson(options: ApplicationOptions): Rule {
189189

190190
if (!options.skipTests) {
191191
rules.push(
192-
...getTestRunnerDependencies(options.testRunner === 'vitest', !!options.skipInstall),
192+
...getTestRunnerDependencies(options.testRunner === TestRunner.Vitest, !!options.skipInstall),
193193
);
194194
}
195195

@@ -343,7 +343,7 @@ function addAppToWorkspaceFile(options: ApplicationOptions, appDir: string): Rul
343343
: {
344344
builder: Builders.BuildUnitTest,
345345
options:
346-
options.testRunner === 'vitest'
346+
options.testRunner === TestRunner.Vitest
347347
? {}
348348
: {
349349
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: 4 additions & 4 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 },
@@ -79,7 +79,7 @@ function addDependenciesToPackageJson({ skipInstall, testRunner }: LibraryOption
7979
install: skipInstall ? InstallBehavior.None : InstallBehavior.Auto,
8080
}),
8181
),
82-
...getTestRunnerDependencies(testRunner === 'vitest', !!skipInstall),
82+
...getTestRunnerDependencies(testRunner === TestRunner.Vitest, !!skipInstall),
8383
addDependency('tslib', latestVersions['tslib'], {
8484
type: DependencyType.Default,
8585
existing: ExistingBehavior.Skip,
@@ -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_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)