forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
134 lines (120 loc) · 4.3 KB
/
index.ts
File metadata and controls
134 lines (120 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {
Rule,
SchematicContext,
SchematicsException,
Tree,
chain,
} from '@angular-devkit/schematics';
import { join } from 'node:path/posix';
import {
DependencyType,
ExistingBehavior,
InstallBehavior,
addDependency,
} from '../utility/dependency';
import { JSONFile } from '../utility/json-file';
import { latestVersions } from '../utility/latest-versions';
import { getWorkspace, updateWorkspace } from '../utility/workspace';
import { Builders } from '../utility/workspace-models';
import { Schema as VitestBrowserOptions } from './schema';
export default function (options: VitestBrowserOptions): Rule {
return async (host: Tree, _context: SchematicContext) => {
const workspace = await getWorkspace(host);
const project = workspace.projects.get(options.project);
if (!project) {
throw new SchematicsException(`Project "${options.project}" does not exist.`);
}
const testTarget = project.targets.get('test');
if (testTarget?.builder !== Builders.BuildUnitTest) {
throw new SchematicsException(
`Project "${options.project}" does not have a "test" target with a supported builder.`,
);
}
if (testTarget.options?.['runner'] === 'karma') {
throw new SchematicsException(
`Project "${options.project}" is configured to use Karma. ` +
'Please migrate to Vitest before adding browser testing support.',
);
}
const packageName = options.package;
if (!packageName) {
return;
}
const dependencies = [packageName];
if (packageName === '@vitest/browser-playwright') {
dependencies.push('playwright');
} else if (packageName === '@vitest/browser-webdriverio') {
dependencies.push('webdriverio');
}
// Update tsconfig.spec.json
const tsConfigPath =
(testTarget.options?.['tsConfig'] as string | undefined) ??
join(project.root, 'tsconfig.spec.json');
const updateTsConfigRule: Rule = (host) => {
if (host.exists(tsConfigPath)) {
const json = new JSONFile(host, tsConfigPath);
const typesPath = ['compilerOptions', 'types'];
const existingTypes = (json.get(typesPath) as string[] | undefined) ?? [];
const newTypes = existingTypes.filter((t) => t !== 'jasmine');
if (!newTypes.includes('vitest/globals')) {
newTypes.push('vitest/globals');
}
if (packageName && !newTypes.includes(packageName)) {
newTypes.push(packageName);
}
if (
newTypes.length !== existingTypes.length ||
newTypes.some((t, i) => t !== existingTypes[i])
) {
json.modify(typesPath, newTypes);
}
}
};
// Determine the default browser based on the provider package
let defaultBrowser: string;
if (packageName === '@vitest/browser-webdriverio') {
defaultBrowser = 'chrome';
} else {
// Playwright and preview both use 'chromium' as the default
defaultBrowser = 'chromium';
}
// Update angular.json to add the browsers option to the test target
const updateAngularJsonRule = updateWorkspace((workspace) => {
const project = workspace.projects.get(options.project);
if (project) {
const testTarget = project.targets.get('test');
if (testTarget) {
testTarget.options ??= {};
const existingBrowsers = testTarget.options['browsers'] as string[] | undefined;
if (!existingBrowsers?.length) {
testTarget.options['browsers'] = [defaultBrowser];
}
}
}
});
return chain([
updateTsConfigRule,
updateAngularJsonRule,
...dependencies.map((name) =>
addDependency(name, latestVersions[name], {
type: DependencyType.Dev,
existing: ExistingBehavior.Skip,
install: options.skipInstall ? InstallBehavior.None : InstallBehavior.Auto,
}),
),
(_, context) => {
context.logger.info(
'Vitest browser testing support has been added. ' +
`The test target has been configured with '${defaultBrowser}' as the default browser.`,
);
},
]);
};
}