forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_spec.ts
476 lines (417 loc) · 20.5 KB
/
index_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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* @license
* Copyright Google Inc. 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.io/license
*/
// tslint:disable:no-big-function
import { JsonParseMode, parseJson } from '@angular-devkit/core';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { latestVersions } from '../utility/latest-versions';
import { getFileContent } from '../utility/test';
import { Schema as WorkspaceOptions } from '../workspace/schema';
import { Schema as ApplicationOptions, Style, ViewEncapsulation } from './schema';
// tslint:disable-next-line: no-any
function readJsonFile(tree: UnitTestTree, path: string): any {
return parseJson(tree.readContent(path).toString(), JsonParseMode.Loose);
}
describe('Application Schematic', () => {
const schematicRunner = new SchematicTestRunner(
'@schematics/angular',
require.resolve('../collection.json'),
);
const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '6.0.0',
};
const defaultOptions: ApplicationOptions = {
name: 'foo',
inlineStyle: false,
inlineTemplate: false,
routing: false,
skipPackageJson: false,
};
let workspaceTree: UnitTestTree;
beforeEach(async () => {
workspaceTree = await schematicRunner.runSchematicAsync('workspace', workspaceOptions).toPromise();
});
it('should create all files of an application', async () => {
const options = { ...defaultOptions };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const files = tree.files;
expect(files).toEqual(jasmine.arrayContaining([
'/projects/foo/karma.conf.js',
'/projects/foo/tsconfig.app.json',
'/projects/foo/tsconfig.spec.json',
'/projects/foo/tslint.json',
'/projects/foo/src/environments/environment.ts',
'/projects/foo/src/environments/environment.prod.ts',
'/projects/foo/src/favicon.ico',
'/projects/foo/src/index.html',
'/projects/foo/src/main.ts',
'/projects/foo/src/polyfills.ts',
'/projects/foo/src/styles.css',
'/projects/foo/src/test.ts',
'/projects/foo/src/app/app.module.ts',
'/projects/foo/src/app/app.component.css',
'/projects/foo/src/app/app.component.html',
'/projects/foo/src/app/app.component.spec.ts',
'/projects/foo/src/app/app.component.ts',
]));
});
it('should add the application to the workspace', async () => {
const options = { ...defaultOptions };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.projects.foo).toBeDefined();
expect(workspace.defaultProject).toBe('foo');
});
it('should add references in solution style tsconfig', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const { references } = readJsonFile(tree, '/tsconfig.json');
expect(references).toEqual([
{ path: './projects/foo/tsconfig.app.json' },
{ path: './projects/foo/tsconfig.spec.json' },
]);
});
it('minimal=true should add correct reference in tsconfig', async () => {
const options = { ...defaultOptions, minimal: true };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const { references } = readJsonFile(tree, '/tsconfig.json');
expect(references).toEqual([
{ path: './projects/foo/tsconfig.app.json' },
]);
});
it('should set the prefix to app if none is set', async () => {
const options = { ...defaultOptions };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.projects.foo.prefix).toEqual('app');
});
it('should set the prefix correctly', async () => {
const options = { ...defaultOptions, prefix: 'pre' };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const workspace = JSON.parse(tree.readContent('/angular.json'));
expect(workspace.projects.foo.prefix).toEqual('pre');
});
it('should handle the routing flag', async () => {
const options = { ...defaultOptions, routing: true };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const files = tree.files;
expect(files).toContain('/projects/foo/src/app/app.module.ts');
expect(files).toContain('/projects/foo/src/app/app-routing.module.ts');
const moduleContent = tree.readContent('/projects/foo/src/app/app.module.ts');
expect(moduleContent).toMatch(/import { AppRoutingModule } from '.\/app-routing.module'/);
const routingModuleContent = tree.readContent('/projects/foo/src/app/app-routing.module.ts');
expect(routingModuleContent).toMatch(/RouterModule.forRoot\(routes\)/);
});
it('should import BrowserModule in the app module', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const path = '/projects/foo/src/app/app.module.ts';
const content = tree.readContent(path);
expect(content).toMatch(/import { BrowserModule } from \'@angular\/platform-browser\';/);
});
it('should declare app component in the app module', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const path = '/projects/foo/src/app/app.module.ts';
const content = tree.readContent(path);
expect(content).toMatch(/import { AppComponent } from \'\.\/app\.component\';/);
});
it(`should set 'defaultEncapsulation' in main.ts when 'ViewEncapsulation' is provided`, async () => {
const tree = await schematicRunner.runSchematicAsync('application', {
...defaultOptions,
viewEncapsulation: ViewEncapsulation.ShadowDom,
}, workspaceTree).toPromise();
const path = '/projects/foo/src/main.ts';
const content = tree.readContent(path);
expect(content).toContain('defaultEncapsulation: ViewEncapsulation.ShadowDom');
expect(content).toContain(`import { enableProdMode, ViewEncapsulation } from '@angular/core'`);
});
it('should set the right paths in the tsconfig.app.json', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.app.json');
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
expect(_extends).toBe('../../tsconfig.base.json');
});
it('should set the right paths in the tsconfig.spec.json', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.spec.json');
expect(files).toEqual(['src/test.ts', 'src/polyfills.ts']);
expect(_extends).toBe('../../tsconfig.base.json');
});
it('should set the right path and prefix in the tslint file', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const path = '/projects/foo/tslint.json';
const content = JSON.parse(tree.readContent(path));
expect(content.extends).toMatch('../../tslint.json');
expect(content.rules['directive-selector'][2]).toMatch('app');
expect(content.rules['component-selector'][2]).toMatch('app');
});
it('should set the right prefix in the tslint file when provided is kebabed', async () => {
const options: ApplicationOptions = { ...defaultOptions, prefix: 'foo-bar' };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const path = '/projects/foo/tslint.json';
const content = JSON.parse(tree.readContent(path));
expect(content.rules['directive-selector'][2]).toMatch('fooBar');
expect(content.rules['component-selector'][2]).toMatch('foo-bar');
});
it('should set the right coverage folder in the karma.json file', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const karmaConf = getFileContent(tree, '/projects/foo/karma.conf.js');
expect(karmaConf).toContain(`dir: require('path').join(__dirname, '../../coverage/foo')`);
});
it('minimal=true should not create e2e, lint and test targets', async () => {
const options = { ...defaultOptions, minimal: true };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const config = JSON.parse(tree.readContent('/angular.json'));
const architect = config.projects.foo.architect;
expect(architect.test).not.toBeDefined();
expect(architect.e2e).not.toBeDefined();
expect(architect.e2e).not.toBeDefined();
});
it('minimal=true should configure the schematics options for components', async () => {
const options = { ...defaultOptions, minimal: true };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const config = JSON.parse(tree.readContent('/angular.json'));
const schematics = config.projects.foo.schematics;
expect(schematics['@schematics/angular:component']).toEqual({
inlineTemplate: true,
inlineStyle: true,
skipTests: true,
});
});
it('should create correct files when using minimal', async () => {
const options = { ...defaultOptions, minimal: true };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const files = tree.files;
[
'/projects/foo/tsconfig.spec.json',
'/projects/foo/tslint.json',
'/projects/foo/karma.conf.js',
'/projects/foo/src/test.ts',
'/projects/foo/src/app/app.component.css',
'/projects/foo/src/app/app.component.html',
'/projects/foo/src/app/app.component.spec.ts',
].forEach(x => expect(files).not.toContain(x));
expect(files).toEqual(jasmine.arrayContaining([
'/projects/foo/tsconfig.app.json',
'/projects/foo/src/environments/environment.ts',
'/projects/foo/src/environments/environment.prod.ts',
'/projects/foo/src/favicon.ico',
'/projects/foo/src/index.html',
'/projects/foo/src/main.ts',
'/projects/foo/src/polyfills.ts',
'/projects/foo/src/styles.css',
'/projects/foo/src/app/app.module.ts',
'/projects/foo/src/app/app.component.ts',
]));
});
describe(`update package.json`, () => {
it(`should add build-angular to devDependencies`, async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const packageJson = JSON.parse(tree.readContent('package.json'));
expect(packageJson.devDependencies['@angular-devkit/build-angular'])
.toEqual(latestVersions.DevkitBuildAngular);
});
it('should use the latest known versions in package.json', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const pkg = JSON.parse(tree.readContent('/package.json'));
expect(pkg.devDependencies['@angular/compiler-cli']).toEqual(latestVersions.Angular);
expect(pkg.devDependencies['typescript']).toEqual(latestVersions.TypeScript);
});
it(`should not override existing users dependencies`, async () => {
const oldPackageJson = workspaceTree.readContent('package.json');
workspaceTree.overwrite('package.json', oldPackageJson.replace(
`"typescript": "${latestVersions.TypeScript}"`,
`"typescript": "~2.5.2"`,
));
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const packageJson = JSON.parse(tree.readContent('package.json'));
expect(packageJson.devDependencies.typescript).toEqual('~2.5.2');
});
it(`should not modify the file when --skipPackageJson`, async () => {
const tree = await schematicRunner.runSchematicAsync('application', {
name: 'foo',
skipPackageJson: true,
}, workspaceTree).toPromise();
const packageJson = JSON.parse(tree.readContent('package.json'));
expect(packageJson.devDependencies['@angular-devkit/build-angular']).toBeUndefined();
});
it('should set the lint tsConfig option', async () => {
const tree = await schematicRunner.runSchematicAsync('application', defaultOptions, workspaceTree)
.toPromise();
const workspace = JSON.parse(tree.readContent('/angular.json'));
const lintOptions = workspace.projects.foo.architect.lint.options;
expect(lintOptions.tsConfig).toEqual([
'projects/foo/tsconfig.app.json',
'projects/foo/tsconfig.spec.json',
'projects/foo/e2e/tsconfig.json',
]);
});
});
it('sideEffects property should be true when strict mode', async () => {
const options = { ...defaultOptions, projectRoot: '', strict: true };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const content = JSON.parse(tree.readContent('/src/app/package.json'));
expect(content.sideEffects).toBe(false);
});
it('sideEffects package.json should not exist when not in strict mode', async () => {
const options = { ...defaultOptions, projectRoot: '', strict: false };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
expect(tree.exists('/src/app/package.json')).toBeFalse();
});
describe('custom projectRoot', () => {
it('should put app files in the right spot', async () => {
const options = { ...defaultOptions, projectRoot: '' };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const files = tree.files;
expect(files).toEqual(jasmine.arrayContaining([
'/karma.conf.js',
'/tsconfig.app.json',
'/tsconfig.spec.json',
'/tslint.json',
'/src/environments/environment.ts',
'/src/environments/environment.prod.ts',
'/src/favicon.ico',
'/src/index.html',
'/src/main.ts',
'/src/polyfills.ts',
'/src/styles.css',
'/src/test.ts',
'/src/app/app.module.ts',
'/src/app/app.component.css',
'/src/app/app.component.html',
'/src/app/app.component.spec.ts',
'/src/app/app.component.ts',
]));
});
it('should set values in angular.json correctly', async () => {
const options = { ...defaultOptions, projectRoot: '' };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const config = JSON.parse(tree.readContent('/angular.json'));
const prj = config.projects.foo;
expect(prj.root).toEqual('');
const buildOpt = prj.architect.build.options;
expect(buildOpt.index).toEqual('src/index.html');
expect(buildOpt.main).toEqual('src/main.ts');
expect(buildOpt.polyfills).toEqual('src/polyfills.ts');
expect(buildOpt.tsConfig).toEqual('tsconfig.app.json');
const testOpt = prj.architect.test.options;
expect(testOpt.main).toEqual('src/test.ts');
expect(testOpt.tsConfig).toEqual('tsconfig.spec.json');
expect(testOpt.karmaConfig).toEqual('karma.conf.js');
expect(testOpt.styles).toEqual([
'src/styles.css',
]);
});
it('should set values in angular.json correctly when using a style preprocessor', async () => {
const options = { ...defaultOptions, projectRoot: '', style: Style.Sass };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const config = JSON.parse(tree.readContent('/angular.json'));
const prj = config.projects.foo;
const buildOpt = prj.architect.build.options;
expect(buildOpt.styles).toEqual([
'src/styles.sass',
]);
const testOpt = prj.architect.test.options;
expect(testOpt.styles).toEqual([
'src/styles.sass',
]);
expect(tree.exists('src/styles.sass')).toBe(true);
});
it('should set the relative tsconfig paths', async () => {
const options = { ...defaultOptions, projectRoot: '' };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const appTsConfig = readJsonFile(tree, '/tsconfig.app.json');
expect(appTsConfig.extends).toEqual('./tsconfig.base.json');
const specTsConfig = readJsonFile(tree, '/tsconfig.spec.json');
expect(specTsConfig.extends).toEqual('./tsconfig.base.json');
expect(specTsConfig.files).toEqual(['src/test.ts', 'src/polyfills.ts']);
});
it('should set the relative path and prefix in the tslint file', async () => {
const options = { ...defaultOptions, projectRoot: '' };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const content = JSON.parse(tree.readContent('/tslint.json'));
expect(content.extends).toMatch('tslint:recommended');
expect(content.rules['directive-selector'][2]).toMatch('app');
expect(content.rules['component-selector'][2]).toMatch('app');
});
it('should merge tslint file', async () => {
const options = { ...defaultOptions, projectRoot: '' };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const content = JSON.parse(tree.readContent('/tslint.json'));
expect(content.extends).toMatch('tslint:recommended');
expect(content.rules['component-selector'][2]).toMatch('app');
expect(content.rules['no-console']).toBeDefined();
// codelyzer rules should be after base tslint rules
expect(Object.keys(content.rules).indexOf('component-selector'))
.toBeGreaterThan(Object.keys(content.rules).indexOf('no-console'));
});
it(`should create correct paths when 'newProjectRoot' is blank`, async () => {
const workspaceTree = await schematicRunner.runSchematicAsync('workspace', { ...workspaceOptions, newProjectRoot: '' }).toPromise();
const options = { ...defaultOptions, projectRoot: undefined };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const config = JSON.parse(tree.readContent('/angular.json'));
const project = config.projects.foo;
expect(project.root).toEqual('foo');
const buildOpt = project.architect.build.options;
expect(buildOpt.index).toEqual('foo/src/index.html');
expect(buildOpt.main).toEqual('foo/src/main.ts');
expect(buildOpt.polyfills).toEqual('foo/src/polyfills.ts');
expect(buildOpt.tsConfig).toEqual('foo/tsconfig.app.json');
const appTsConfig = readJsonFile(tree, '/foo/tsconfig.app.json');
expect(appTsConfig.extends).toEqual('../tsconfig.base.json');
const specTsConfig = readJsonFile(tree, '/foo/tsconfig.spec.json');
expect(specTsConfig.extends).toEqual('../tsconfig.base.json');
});
});
it(`should add support for IE 9-11 in '.browserslistrc' when 'legacyBrowsers' is true`, async () => {
const options: ApplicationOptions = { ...defaultOptions, legacyBrowsers: true };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const content = tree.readContent('/projects/foo/.browserslistrc');
expect(content).not.toContain('not IE 11');
expect(content).toContain('IE 11');
expect(content).not.toContain('not IE 9-10');
expect(content).toContain('IE 9-10');
});
it(`should not add support for IE 9-11 in '.browserslistrc' when 'legacyBrowsers' is false`, async () => {
const options: ApplicationOptions = { ...defaultOptions, legacyBrowsers: false };
const tree = await schematicRunner.runSchematicAsync('application', options, workspaceTree)
.toPromise();
const content = tree.readContent('/projects/foo/.browserslistrc');
expect(content).toContain('not IE 11');
expect(content).toContain('not IE 9-10');
});
});