forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild_spec.ts
623 lines (561 loc) · 21.3 KB
/
rebuild_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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/**
* @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 { Architect } from '@angular-devkit/architect';
import { join, logging, normalize, virtualFs } from '@angular-devkit/core';
import { debounceTime, take, takeWhile, tap } from 'rxjs/operators';
import {
createArchitect,
host,
lazyModuleFiles,
lazyModuleFnImport,
outputPath,
veEnabled,
} from '../../test-utils';
describe('Browser Builder rebuilds', () => {
const target = { project: 'app', target: 'build' };
// Rebuild tests are especially sensitive to time between writes due to file watcher
// behaviour. Give them a while.
const rebuildDebounceTime = 3000;
let architect: Architect;
beforeEach(async () => {
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;
});
afterEach(async () => host.restore().toPromise());
it('rebuilds on TS file changes', async () => {
const goldenValueFiles: { [path: string]: string } = {
'src/app/app.module.ts': `
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
console.log('$$_E2E_GOLDEN_VALUE_1');
export let X = '$$_E2E_GOLDEN_VALUE_2';
`,
'src/main.ts': `
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);
import * as m from './app/app.module';
console.log(m.X);
console.log('$$_E2E_GOLDEN_VALUE_3');
`,
};
const overrides = { watch: true };
let phase = 1;
const run = await architect.scheduleTarget(target, overrides);
await run.output
.pipe(
debounceTime(rebuildDebounceTime),
tap(result => {
expect(result.success).toBe(true, 'build should succeed');
const hasLazyChunk = host.scopedSync().exists(normalize('dist/lazy-lazy-module.js'));
switch (phase) {
case 1:
// No lazy chunk should exist.
if (!hasLazyChunk) {
phase = 2;
host.writeMultipleFiles({ ...lazyModuleFiles, ...lazyModuleFnImport });
}
break;
case 2:
// A lazy chunk should have been with the filename.
if (hasLazyChunk) {
phase = 3;
host.writeMultipleFiles(goldenValueFiles);
}
break;
case 3:
// The golden values should be present and in the right order.
const re = new RegExp(
/\$\$_E2E_GOLDEN_VALUE_1(.|\n|\r)*/.source +
/\$\$_E2E_GOLDEN_VALUE_2(.|\n|\r)*/.source +
/\$\$_E2E_GOLDEN_VALUE_3/.source,
);
const fileName = './dist/main.js';
const content = virtualFs.fileBufferToString(
host.scopedSync().read(normalize(fileName)),
);
if (re.test(content)) {
phase = 4;
}
break;
}
}),
takeWhile(() => phase < 4),
)
.toPromise();
await run.stop();
});
it('rebuilds on CSS changes', async () => {
const overrides = { watch: true };
const run = await architect.scheduleTarget(target, overrides);
await run.output
.pipe(
debounceTime(rebuildDebounceTime),
tap(buildEvent => expect(buildEvent.success).toBe(true)),
tap(() => host.appendToFile('src/app/app.component.css', ':host { color: blue; }')),
take(2),
)
.toPromise();
await run.stop();
});
it('type checks on rebuilds', async () => {
host.writeMultipleFiles({
'src/funky2.ts': `export const funky2 = (value: string) => value + 'hello';`,
'src/funky.ts': `export * from './funky2';`,
});
host.appendToFile(
'src/main.ts',
`
import { funky2 } from './funky';
console.log(funky2('town'));
`,
);
const overrides = { watch: true, forkTypeChecker: false };
const logger = new logging.Logger('');
let logs: string[] = [];
logger.subscribe(e => logs.push(e.message));
const typeError = `is not assignable to parameter of type 'number'`;
let buildNumber = 0;
const run = await architect.scheduleTarget(target, overrides, { logger });
await run.output
.pipe(
debounceTime(rebuildDebounceTime),
tap(buildEvent => {
buildNumber += 1;
switch (buildNumber) {
case 1:
expect(buildEvent.success).toBe(true);
// Make an invalid version of the file.
// Should trigger a rebuild, this time an error is expected.
host.writeMultipleFiles({
'src/funky2.ts': `export const funky2 = (value: number) => value + 1;`,
});
break;
case 2:
// The second build should error out with a type error on the type of an argument.
expect(buildEvent.success).toBe(false);
expect(logs.join().includes(typeError)).toBe(true);
logs = [];
// Change an UNRELATED file and the error should still happen.
// Should trigger a rebuild, this time an error is also expected.
host.appendToFile('src/app/app.module.ts', `console.log(1);`);
break;
case 3:
// The third build should have the same error as the first.
expect(buildEvent.success).toBe(false);
expect(logs.join().includes(typeError)).toBe(true);
logs = [];
// Fix the error.
host.writeMultipleFiles({
'src/funky2.ts': `export const funky2 = (value: string) => value + 'hello';`,
});
break;
default:
expect(buildEvent.success).toBe(true);
break;
}
}),
take(4),
)
.toPromise();
await run.stop();
});
it('rebuilds on type changes', async () => {
host.writeMultipleFiles({ 'src/type.ts': `export type MyType = number;` });
host.appendToFile('src/main.ts', `import { MyType } from './type';`);
const overrides = { watch: true };
const run = await architect.scheduleTarget(target, overrides);
await run.output
.pipe(
debounceTime(rebuildDebounceTime),
tap(buildEvent => expect(buildEvent.success).toBe(true)),
tap(() => host.writeMultipleFiles({ 'src/type.ts': `export type MyType = string;` })),
take(2),
)
.toPromise();
});
it('rebuilds on transitive type-only file changes', async () => {
if (veEnabled) {
// TODO: https://github.com/angular/angular-cli/issues/15056
pending('Only supported in Ivy.');
return;
}
host.writeMultipleFiles({
'src/interface1.ts': `
import { Interface2 } from './interface2';
export interface Interface1 extends Interface2 { }
`,
'src/interface2.ts': `
import { Interface3 } from './interface3';
export interface Interface2 extends Interface3 { }
`,
'src/interface3.ts': `export interface Interface3 { nbr: number; }`,
});
host.appendToFile('src/main.ts', `
import { Interface1 } from './interface1';
const something: Interface1 = { nbr: 43 };
`);
const overrides = { watch: true };
const run = await architect.scheduleTarget(target, overrides);
let buildNumber = 0;
await run.output
.pipe(
debounceTime(rebuildDebounceTime),
tap(buildEvent => expect(buildEvent.success).toBe(true)),
tap(() => {
// NOTE: this only works for transitive type deps after the first build, and only if the
// typedep file was there on the previous build.
// Make sure the first rebuild is triggered on a direct dep (typedep or not).
buildNumber++;
if (buildNumber < 4) {
host.appendToFile(`src/interface${buildNumber}.ts`, `export type MyType = string;`);
} else {
host.appendToFile(`src/typings.d.ts`, `export type MyType = string;`);
}
}),
take(5),
)
.toPromise();
});
it('rebuilds on transitive non node package DTS file changes', async () => {
host.writeMultipleFiles({
'src/interface1.d.ts': `
import { Interface2 } from './interface2';
export interface Interface1 extends Interface2 { }
`,
'src/interface2.d.ts': `
import { Interface3 } from './interface3';
export interface Interface2 extends Interface3 { }
`,
'src/interface3.d.ts': `export interface Interface3 { nbr: number; }`,
});
host.appendToFile('src/main.ts', `
import { Interface1 } from './interface1';
const something: Interface1 = { nbr: 43 };
`);
const overrides = { watch: true };
const run = await architect.scheduleTarget(target, overrides);
let buildNumber = 0;
await run.output
.pipe(
debounceTime(rebuildDebounceTime),
tap(buildEvent => expect(buildEvent.success).toBe(true)),
tap(() => {
buildNumber++;
if (buildNumber === 1) {
host.appendToFile('src/interface3.d.ts', 'export declare type MyType = string;');
}
}),
take(2),
)
.toPromise();
});
it('rebuilds after errors in JIT', async () => {
const origContent = virtualFs.fileBufferToString(
host.scopedSync().read(normalize('src/app/app.component.ts')),
);
host.appendToFile('./src/app/app.component.ts', `]]]]`);
const overrides = { watch: true, aot: false };
let buildNumber = 0;
const run = await architect.scheduleTarget(target, overrides);
await run.output
.pipe(
debounceTime(rebuildDebounceTime),
tap(buildEvent => {
buildNumber++;
switch (buildNumber) {
case 1:
// The first build should fail.
expect(buildEvent.success).toBe(false);
// Fix the error.
host.writeMultipleFiles({'src/app/app.component.ts': origContent});
break;
case 2:
// The second build should have everything fixed.
expect(buildEvent.success).toBe(true);
break;
}
}),
take(2),
)
.toPromise();
await run.stop();
});
it('rebuilds after errors in AOT', async () => {
// Save the original contents of `./src/app/app.component.ts`.
const origContent = virtualFs.fileBufferToString(
host.scopedSync().read(normalize('src/app/app.component.ts')),
);
// Add a major static analysis error on a non-main file to the initial build.
host.replaceInFile('./src/app/app.component.ts', `'app-root'`, `(() => 'app-root')()`);
// `selector must be a string` errors on VE are part of the emit result, but on Ivy they only
// show up in getNgSemanticDiagnostics. Since getNgSemanticDiagnostics is only called on the
// type checker, we must disable it to get a failing fourth build with Ivy.
const overrides = { watch: true, aot: true, forkTypeChecker: veEnabled };
const logger = new logging.Logger('');
let logs: string[] = [];
logger.subscribe(e => logs.push(e.message));
const staticAnalysisError = !veEnabled
? 'selector must be a string'
: 'Function expressions are not supported in decorators';
const syntaxError = 'Declaration or statement expected.';
let buildNumber = 0;
const run = await architect.scheduleTarget(target, overrides, { logger });
await run.output
.pipe(
debounceTime(rebuildDebounceTime),
tap(buildEvent => {
buildNumber += 1;
switch (buildNumber) {
case 1:
// The first build should error out with a static analysis error.
expect(buildEvent.success).toBe(false, 'First build should not succeed.');
expect(logs.join().includes(staticAnalysisError)).toBe(true,
'First build should have static analysis error.');
logs = [];
// Fix the static analysis error.
host.writeMultipleFiles({ 'src/app/app.component.ts': origContent });
break;
case 2:
expect(buildEvent.success).toBe(true, 'Second build should succeed.');
expect(logs.join().includes(staticAnalysisError)).toBe(false,
'Second build should not have static analysis error.');
logs = [];
// Add an syntax error to a non-main file.
host.appendToFile('src/app/app.component.ts', `]]]`);
break;
case 3:
// The third build should have TS syntax error.
expect(buildEvent.success).toBe(false, 'Third build should not succeed.');
expect(logs.join().includes(syntaxError)).toBe(true,
'Third build should have syntax analysis error.');
expect(logs.join().includes(staticAnalysisError)).toBe(false,
'Third build should not have static analysis error.');
logs = [];
// Fix the syntax error, but add the static analysis error again.
host.writeMultipleFiles({
'src/app/app.component.ts': origContent.replace(
`'app-root'`,
`(() => 'app-root')()`,
),
});
break;
case 4:
expect(buildEvent.success).toBe(false, 'Fourth build should not succeed.');
expect(logs.join().includes(syntaxError)).toBe(false,
'Fourth build should not have syntax analysis error.');
expect(logs.join().includes(staticAnalysisError)).toBe(true,
'Fourth build should have static analysis error.');
logs = [];
// Restore the file to a error-less state.
host.writeMultipleFiles({ 'src/app/app.component.ts': origContent });
break;
case 5:
// The fifth build should have everything fixed..
expect(buildEvent.success).toBe(true, 'Fifth build should succeed.');
expect(logs.join().includes(syntaxError)).toBe(false,
'Fifth build should not have syntax analysis error.');
expect(logs.join().includes(staticAnalysisError)).toBe(false,
'Fifth build should not have static analysis error.');
break;
}
}),
take(5),
)
.toPromise();
await run.stop();
});
it('rebuilds AOT factories', async () => {
host.writeMultipleFiles({
'src/app/app.component.css': `
@import './imported-styles.css';
body {background-color: #00f;}
`,
'src/app/imported-styles.css': 'p {color: #f00;}',
});
const overrides = { watch: true, aot: true };
let buildNumber = 0;
const run = await architect.scheduleTarget(target, overrides);
await run.output
.pipe(
debounceTime(rebuildDebounceTime),
tap(buildEvent => {
buildNumber += 1;
const fileName = './dist/main.js';
let content;
switch (buildNumber) {
case 1:
// Trigger a few rebuilds first.
// The AOT compiler is still optimizing rebuilds on the first rebuilds.
expect(buildEvent.success).toBe(true);
host.appendToFile('src/main.ts', 'console.log(1);');
break;
case 2:
expect(buildEvent.success).toBe(true);
host.appendToFile('src/main.ts', 'console.log(1);');
break;
case 3:
// Change the component html.
expect(buildEvent.success).toBe(true);
host.appendToFile('src/app/app.component.html', '<p>HTML_REBUILD_STRING<p>');
break;
case 4:
// Check if html changes are added to factories.
expect(buildEvent.success).toBe(true);
content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
expect(content).toContain('HTML_REBUILD_STRING');
// Change the component css.
host.appendToFile('src/app/app.component.css', 'CSS_REBUILD_STRING {color: #f00;}');
break;
case 5:
// Check if css changes are added to factories.
expect(buildEvent.success).toBe(true);
content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
expect(content).toContain('CSS_REBUILD_STRING');
// Change the component css import.
host.appendToFile(
'src/app/imported-styles.css',
'CSS_DEP_REBUILD_STRING {color: #f00;}',
);
break;
case 6:
// Check if css import changes are added to factories.
expect(buildEvent.success).toBe(true);
content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
expect(content).toContain('CSS_DEP_REBUILD_STRING');
// Change the component itself.
host.replaceInFile(
'src/app/app.component.ts',
'app-root',
'app-root-FACTORY_REBUILD_STRING',
);
break;
case 7:
// Check if component changes are added to factories.
expect(buildEvent.success).toBe(true);
content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
expect(content).toContain('FACTORY_REBUILD_STRING');
break;
}
}),
take(7),
)
.toPromise();
await run.stop();
});
it('rebuilds on changes in barrel file dependency', async () => {
host.writeMultipleFiles({
'src/index.ts': `export * from './interface'`,
'src/interface.ts': `export interface Foo { bar: boolean };`,
});
host.appendToFile(
'src/main.ts',
`
import { Foo } from './index';
const x: Foo = { bar: true };
`,
);
const overrides = { watch: true, aot: false };
let buildNumber = 0;
const run = await architect.scheduleTarget(target, overrides);
await run.output
.pipe(
debounceTime(rebuildDebounceTime),
tap(buildEvent => {
buildNumber += 1;
switch (buildNumber) {
case 1:
expect(buildEvent.success).toBe(true);
host.writeMultipleFiles({
'src/interface.ts': `export interface Foo {
bar: boolean;
baz?: string;
};`,
});
break;
case 2:
expect(buildEvent.success).toBe(true);
break;
}
}),
take(2),
)
.toPromise();
await run.stop();
});
it('rebuilds AOT on CSS changes', async () => {
const overrides = { watch: true, aot: true };
let buildCount = 1;
const run = await architect.scheduleTarget(target, overrides);
await run.output.pipe(
debounceTime(rebuildDebounceTime),
tap(() => {
const content = virtualFs.fileBufferToString(
host.scopedSync().read(join(outputPath, 'main.js')),
);
switch (buildCount) {
case 1:
expect(content).not.toContain('color: green');
host.appendToFile('src/app/app.component.css', 'h1 { color: green; }');
break;
case 2:
expect(content).toContain('color: green');
break;
}
buildCount++;
}),
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
take(2),
).toPromise();
await run.stop();
});
it('rebuilds AOT on HTML changes', async () => {
const overrides = { watch: true, aot: true };
let buildCount = 1;
const run = await architect.scheduleTarget(target, overrides);
await run.output.pipe(
debounceTime(rebuildDebounceTime),
tap(() => {
const content = virtualFs.fileBufferToString(
host.scopedSync().read(join(outputPath, 'main.js')),
);
switch (buildCount) {
case 1:
expect(content).not.toContain('New Updated Content');
host.appendToFile('src/app/app.component.html', 'New Updated Content');
break;
case 2:
expect(content).toContain('New Updated Content');
break;
}
buildCount++;
}),
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
take(2),
).toPromise();
await run.stop();
});
});