/
karma-project.spec.ts
278 lines (238 loc) · 9.64 KB
/
karma-project.spec.ts
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import type { Tree } from '@nrwl/devkit';
import * as devkit from '@nrwl/devkit';
import {
readProjectConfiguration,
updateProjectConfiguration,
} from '@nrwl/devkit';
import { createTreeWithEmptyV1Workspace } from '@nrwl/devkit/testing';
import { karmaProjectGenerator } from './karma-project';
import libraryGenerator from '../library/library';
import { Linter } from '@nrwl/linter';
import { UnitTestRunner } from '../../utils/test-runners';
import applicationGenerator from '../application/application';
describe('karmaProject', () => {
let tree: Tree;
beforeEach(async () => {
tree = createTreeWithEmptyV1Workspace();
await libraryGenerator(tree, {
name: 'lib1',
buildable: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: false,
skipFormat: false,
unitTestRunner: UnitTestRunner.None,
});
await applicationGenerator(tree, {
name: 'app1',
unitTestRunner: UnitTestRunner.None,
});
});
it('should throw when there is already a test target', async () => {
devkit.updateJson(tree, 'workspace.json', (json) => {
json.projects['lib1'].architect.test = {};
return json;
});
await expect(
karmaProjectGenerator(tree, { project: 'lib1' })
).rejects.toThrow('"lib1" already has a test target.');
});
it('should generate files', async () => {
expect(tree.exists('karma.conf.js')).toBeFalsy();
await karmaProjectGenerator(tree, { project: 'lib1' });
expect(tree.exists('/libs/lib1/karma.conf.js')).toBeTruthy();
expect(tree.exists('/libs/lib1/tsconfig.spec.json')).toBeTruthy();
expect(
tree.read('/libs/lib1/tsconfig.spec.json', 'utf-8')
).toMatchSnapshot();
expect(tree.exists('karma.conf.js')).toBeTruthy();
});
it('should generate files and not add polyfills if it is using ng v15 style polyfills', async () => {
expect(tree.exists('karma.conf.js')).toBeFalsy();
await karmaProjectGenerator(tree, { project: 'app1' });
expect(tree.exists('/apps/app1/tsconfig.spec.json')).toBeTruthy();
expect(
tree.read('/apps/app1/tsconfig.spec.json', 'utf-8')
).toMatchSnapshot();
});
it('should generate files and correctly add polyfills if it is using old ng style polyfills', async () => {
tree.write('apps/app1/src/polyfills.ts', 'import zone.js;');
const project = readProjectConfiguration(tree, 'app1');
project.targets.build.options.polyfills = 'apps/app1/src/polyfills.ts';
updateProjectConfiguration(tree, 'app1', project);
expect(tree.exists('karma.conf.js')).toBeFalsy();
await karmaProjectGenerator(tree, { project: 'app1' });
expect(tree.exists('/apps/app1/tsconfig.spec.json')).toBeTruthy();
expect(
tree.read('/apps/app1/tsconfig.spec.json', 'utf-8')
).toMatchSnapshot();
});
it('should create a karma.conf.js', async () => {
await karmaProjectGenerator(tree, { project: 'lib1' });
const karmaConf = tree.read('libs/lib1/karma.conf.js').toString();
expect(karmaConf).toMatchSnapshot();
});
it('should update the project tsconfig.json to reference the tsconfig.spec.json', async () => {
await karmaProjectGenerator(tree, { project: 'lib1' });
const tsConfig = devkit.readJson(tree, 'libs/lib1/tsconfig.json');
expect(tsConfig.references).toContainEqual({
path: './tsconfig.spec.json',
});
});
it('should format files', async () => {
jest.spyOn(devkit, 'formatFiles');
await karmaProjectGenerator(tree, { project: 'lib1' });
expect(devkit.formatFiles).toHaveBeenCalled();
});
describe('library', () => {
it('should update the workspace config correctly', async () => {
await karmaProjectGenerator(tree, { project: 'lib1' });
const workspaceJson = devkit.readJson(tree, 'workspace.json');
expect(workspaceJson.projects.lib1.architect.test).toEqual({
builder: '@angular-devkit/build-angular:karma',
options: {
tsConfig: 'libs/lib1/tsconfig.spec.json',
karmaConfig: 'libs/lib1/karma.conf.js',
polyfills: ['zone.js', 'zone.js/testing'],
},
});
});
it('should create a tsconfig.spec.json', async () => {
await karmaProjectGenerator(tree, { project: 'lib1' });
const tsConfig = devkit.readJson(tree, 'libs/lib1/tsconfig.spec.json');
expect(tsConfig).toEqual({
extends: './tsconfig.json',
compilerOptions: {
outDir: '../../dist/out-tsc',
types: ['jasmine', 'node'],
},
include: ['**/*.spec.ts', '**/*.test.ts', '**/*.d.ts'],
});
});
});
describe('applications', () => {
it('should update the workspace config correctly', async () => {
await karmaProjectGenerator(tree, { project: 'app1' });
const workspaceJson = devkit.readJson(tree, 'workspace.json');
expect(workspaceJson.projects.app1.architect.test).toEqual({
builder: '@angular-devkit/build-angular:karma',
options: {
polyfills: ['zone.js', 'zone.js/testing'],
tsConfig: 'apps/app1/tsconfig.spec.json',
karmaConfig: 'apps/app1/karma.conf.js',
styles: [],
scripts: [],
assets: [],
},
});
});
it('should create a tsconfig.spec.json', async () => {
await karmaProjectGenerator(tree, { project: 'app1' });
const tsConfig = devkit.readJson(tree, 'apps/app1/tsconfig.spec.json');
expect(tsConfig).toEqual({
extends: './tsconfig.json',
compilerOptions: {
outDir: '../../dist/out-tsc',
types: ['jasmine', 'node'],
},
include: ['**/*.spec.ts', '**/*.test.ts', '**/*.d.ts'],
});
});
it('should update the workspace config correctly when using old style ng polyfills', async () => {
tree.write('apps/app1/src/polyfills.ts', 'import zone.js;');
const project = readProjectConfiguration(tree, 'app1');
project.targets.build.options.polyfills = 'apps/app1/src/polyfills.ts';
updateProjectConfiguration(tree, 'app1', project);
await karmaProjectGenerator(tree, { project: 'app1' });
const workspaceJson = devkit.readJson(tree, 'workspace.json');
expect(workspaceJson.projects.app1.architect.test).toEqual({
builder: '@angular-devkit/build-angular:karma',
options: {
polyfills: 'apps/app1/src/polyfills.ts',
tsConfig: 'apps/app1/tsconfig.spec.json',
karmaConfig: 'apps/app1/karma.conf.js',
styles: [],
scripts: [],
assets: [],
},
});
});
it('should create a tsconfig.spec.json when using old style ng polyfills', async () => {
tree.write('apps/app1/src/polyfills.ts', 'import zone.js;');
const project = readProjectConfiguration(tree, 'app1');
project.targets.build.options.polyfills = 'apps/app1/src/polyfills.ts';
updateProjectConfiguration(tree, 'app1', project);
await karmaProjectGenerator(tree, { project: 'app1' });
const tsConfig = devkit.readJson(tree, 'apps/app1/tsconfig.spec.json');
expect(tsConfig).toEqual({
extends: './tsconfig.json',
compilerOptions: {
outDir: '../../dist/out-tsc',
types: ['jasmine', 'node'],
},
files: ['src/polyfills.ts'],
include: ['**/*.spec.ts', '**/*.test.ts', '**/*.d.ts'],
});
});
});
describe('--root-project', () => {
it('should support a project located at the root', async () => {
await applicationGenerator(tree, {
name: 'root-app',
unitTestRunner: UnitTestRunner.None,
rootProject: true,
});
await karmaProjectGenerator(tree, { project: 'root-app' });
expect(tree.exists('karma.conf.js')).toBe(true);
expect(tree.read('karma.conf.js', 'utf-8')).toMatchSnapshot();
expect(tree.exists('tsconfig.spec.json')).toBe(true);
const { references } = devkit.readJson(tree, 'tsconfig.json');
expect(references).toContainEqual({
path: './tsconfig.spec.json',
});
const project = devkit.readProjectConfiguration(tree, 'root-app');
expect(project.targets.test.options).toStrictEqual({
polyfills: ['zone.js', 'zone.js/testing'],
tsConfig: 'tsconfig.spec.json',
karmaConfig: 'karma.conf.js',
styles: [],
scripts: [],
assets: [],
});
});
it('should generate the right karma.conf.js file for a nested project in a workspace with a project at the root', async () => {
await applicationGenerator(tree, {
name: 'root-app',
unitTestRunner: UnitTestRunner.Karma,
rootProject: true,
});
await libraryGenerator(tree, {
name: 'nested-lib',
unitTestRunner: UnitTestRunner.None,
});
await karmaProjectGenerator(tree, { project: 'nested-lib' });
expect(tree.exists('libs/nested-lib/karma.conf.js')).toBe(true);
expect(
tree.read('libs/nested-lib/karma.conf.js', 'utf-8')
).toMatchSnapshot();
expect(tree.read('karma.conf.js', 'utf-8')).toMatchSnapshot();
});
it('should use get syntax when root project does not have karma conf with set syntax', async () => {
await applicationGenerator(tree, {
name: 'root-app',
unitTestRunner: UnitTestRunner.Jest,
rootProject: true,
});
await libraryGenerator(tree, {
name: 'nested-lib',
unitTestRunner: UnitTestRunner.None,
});
await karmaProjectGenerator(tree, { project: 'nested-lib' });
expect(tree.exists('libs/nested-lib/karma.conf.js')).toBe(true);
expect(
tree.read('libs/nested-lib/karma.conf.js', 'utf-8')
).toMatchSnapshot();
expect(tree.read('karma.conf.js', 'utf-8')).toMatchSnapshot();
});
});
});