forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_spec.ts
More file actions
180 lines (148 loc) · 6.06 KB
/
index_spec.ts
File metadata and controls
180 lines (148 loc) · 6.06 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* @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 { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { parse } from 'jsonc-parser';
describe('Vitest Browser Provider Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@schematics/angular',
require.resolve('../collection.json'),
);
let tree: UnitTestTree;
beforeEach(async () => {
tree = await schematicRunner.runSchematic('workspace', {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
});
tree = await schematicRunner.runSchematic(
'application',
{
name: 'app',
skipInstall: true,
testRunner: 'vitest',
},
tree,
);
});
it('should add dependencies and update tsconfig.spec.json', async () => {
const options = {
project: 'app',
package: '@vitest/browser-playwright',
skipInstall: true,
};
const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree);
const packageJson = parse(resultTree.readContent('/package.json'));
expect(packageJson.devDependencies['@vitest/browser-playwright']).toBeDefined();
expect(packageJson.devDependencies['playwright']).toBeDefined();
const tsConfig = parse(resultTree.readContent('/projects/app/tsconfig.spec.json'));
expect(tsConfig.compilerOptions.types).toContain('vitest/globals');
expect(tsConfig.compilerOptions.types).toContain('@vitest/browser-playwright');
expect(tsConfig.compilerOptions.types).not.toContain('jasmine');
});
it('should add browsers option to angular.json for playwright', async () => {
const options = {
project: 'app',
package: '@vitest/browser-playwright',
skipInstall: true,
};
const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree);
const angularJson = parse(resultTree.readContent('/angular.json'));
const project = angularJson.projects.app;
const targets = project.architect || project.targets;
expect(targets.test.options.browsers).toEqual(['chromium']);
});
it('should add browsers option to angular.json for webdriverio', async () => {
const options = {
project: 'app',
package: '@vitest/browser-webdriverio',
skipInstall: true,
};
const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree);
const angularJson = parse(resultTree.readContent('/angular.json'));
const project = angularJson.projects.app;
const targets = project.architect || project.targets;
expect(targets.test.options.browsers).toEqual(['chrome']);
});
it('should not overwrite existing browsers option in angular.json', async () => {
// Set up existing browsers option
const angularJson = parse(tree.readContent('/angular.json'));
const project = angularJson.projects.app;
const targets = project.architect || project.targets;
targets.test.options ??= {};
targets.test.options.browsers = ['firefox'];
tree.overwrite('/angular.json', JSON.stringify(angularJson));
const options = {
project: 'app',
package: '@vitest/browser-playwright',
skipInstall: true,
};
const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree);
const updatedAngularJson = parse(resultTree.readContent('/angular.json'));
const updatedProject = updatedAngularJson.projects.app;
const updatedTargets = updatedProject.architect || updatedProject.targets;
expect(updatedTargets.test.options.browsers).toEqual(['firefox']);
});
it('should add webdriverio dependency when @vitest/browser-webdriverio is used', async () => {
const options = {
project: 'app',
package: '@vitest/browser-webdriverio',
skipInstall: true,
};
const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree);
const packageJson = parse(resultTree.readContent('/package.json'));
expect(packageJson.devDependencies['@vitest/browser-webdriverio']).toBeDefined();
expect(packageJson.devDependencies['webdriverio']).toBeDefined();
});
it('should update tsconfig.spec.json for a library project', async () => {
tree = await schematicRunner.runSchematic(
'library',
{
name: 'lib',
skipInstall: true,
},
tree,
);
const options = {
project: 'lib',
package: '@vitest/browser-playwright',
skipInstall: true,
};
const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree);
const tsConfig = parse(resultTree.readContent('/projects/lib/tsconfig.spec.json'));
expect(tsConfig.compilerOptions.types).toContain('vitest/globals');
expect(tsConfig.compilerOptions.types).toContain('@vitest/browser-playwright');
// Library schematic might put jasmine types by default.
expect(tsConfig.compilerOptions.types).not.toContain('jasmine');
});
it('should throw if project does not exist', async () => {
const options = {
project: 'invalid',
package: '@vitest/browser-playwright',
};
await expectAsync(
schematicRunner.runSchematic('vitest-browser', options, tree),
).toBeRejectedWithError('Project "invalid" does not exist.');
});
it('should throw if project uses Karma', async () => {
const angularJson = parse(tree.readContent('/angular.json'));
const project = angularJson.projects.app;
const targets = project.architect || project.targets;
targets.test.options ??= {};
targets.test.options.runner = 'karma';
tree.overwrite('/angular.json', JSON.stringify(angularJson));
const options = {
project: 'app',
package: '@vitest/browser-playwright',
};
await expectAsync(
schematicRunner.runSchematic('vitest-browser', options, tree),
).toBeRejectedWithError(
'Project "app" is configured to use Karma. Please migrate to Vitest before adding browser testing support.',
);
});
});