Skip to content

Commit b403aa6

Browse files
clydinKeen Yee Liau
authored and
Keen Yee Liau
committed
test(@angular-devkit/build-angular): improve resilience of watch rebuilds
1 parent 96dc506 commit b403aa6

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

packages/angular_devkit/build_angular/test/browser/rebuild_spec_large.ts

+23-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import { DefaultTimeout, TestLogger, runTargetSpec } from '@angular-devkit/architect/testing';
1111
import { join, normalize, virtualFs } from '@angular-devkit/core';
12-
import { debounceTime, take, tap } from 'rxjs/operators';
12+
import { debounceTime, take, takeWhile, tap } from 'rxjs/operators';
1313
import { browserTargetSpec, host } from '../utils';
1414
import { lazyModuleFiles, lazyModuleImport } from './lazy-module_spec_large';
1515

@@ -65,29 +65,28 @@ describe('Browser Builder rebuilds', () => {
6565

6666
const overrides = { watch: true };
6767

68-
let buildNumber = 0;
69-
68+
let buildCount = 0;
69+
let phase = 1;
7070
runTargetSpec(host, browserTargetSpec, overrides, DefaultTimeout * 3).pipe(
71-
// We must debounce on watch mode because file watchers are not very accurate.
72-
// Changes from just before a process runs can be picked up and cause rebuilds.
73-
// In this case, cleanup from the test right before this one causes a few rebuilds.
74-
debounceTime(1000),
75-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
71+
tap((buildEvent) => expect(buildEvent.success).toBe(true, 'build should succeed')),
7672
tap(() => {
77-
buildNumber += 1;
78-
switch (buildNumber) {
73+
buildCount++;
74+
const hasLazyChunk = host.scopedSync().exists(join(outputPath, 'lazy-lazy-module.js'));
75+
switch (phase) {
7976
case 1:
8077
// No lazy chunk should exist.
81-
expect(host.scopedSync().exists(join(outputPath, 'lazy-module.js'))).toBe(false);
82-
// Write the lazy chunk files. Order matters when writing these, because of imports.
83-
host.writeMultipleFiles(lazyModuleFiles);
84-
host.writeMultipleFiles(lazyModuleImport);
78+
if (!hasLazyChunk) {
79+
phase = 2;
80+
host.writeMultipleFiles({ ...lazyModuleFiles, ...lazyModuleImport });
81+
}
8582
break;
8683

8784
case 2:
8885
// A lazy chunk should have been with the filename.
89-
expect(host.scopedSync().exists(join(outputPath, 'lazy-lazy-module.js'))).toBe(true);
90-
host.writeMultipleFiles(goldenValueFiles);
86+
if (hasLazyChunk) {
87+
phase = 3;
88+
host.writeMultipleFiles(goldenValueFiles);
89+
}
9190
break;
9291

9392
case 3:
@@ -101,15 +100,18 @@ describe('Browser Builder rebuilds', () => {
101100
const content = virtualFs.fileBufferToString(
102101
host.scopedSync().read(normalize(fileName)),
103102
);
104-
expect(content).toMatch(re);
105-
break;
106103

107-
default:
104+
if (re.test(content)) {
105+
phase = 4;
106+
}
108107
break;
109108
}
110109
}),
111-
take(3),
112-
).toPromise().then(done, done.fail);
110+
takeWhile(() => phase < 4),
111+
).toPromise().then(
112+
() => done(),
113+
() => done.fail(`stuck at phase ${phase} [builds: ${buildCount}]`),
114+
);
113115
});
114116

115117
it('rebuilds on CSS changes', (done) => {

packages/angular_devkit/build_angular/test/browser/replacements_spec_large.ts

+31-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import { runTargetSpec } from '@angular-devkit/architect/testing';
1010
import { join, normalize, virtualFs } from '@angular-devkit/core';
11-
import { debounceTime, take, tap } from 'rxjs/operators';
11+
import { takeWhile, tap } from 'rxjs/operators';
1212
import { browserTargetSpec, host } from '../utils';
1313

1414

@@ -112,33 +112,46 @@ describe('Browser Builder file replacements', () => {
112112
watch: true,
113113
};
114114

115-
let buildNumber = 0;
116-
117-
runTargetSpec(host, browserTargetSpec, overrides, 45000).pipe(
118-
debounceTime(1000),
119-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
115+
let buildCount = 0;
116+
let phase = 1;
117+
runTargetSpec(host, browserTargetSpec, overrides, 30000).pipe(
118+
tap((buildEvent) => expect(buildEvent.success).toBe(true, 'build should succeed')),
120119
tap(() => {
121120
const fileName = join(outputPath, 'main.js');
122121
const content = virtualFs.fileBufferToString(host.scopedSync().read(fileName));
123-
buildNumber += 1;
124-
125-
switch (buildNumber) {
122+
const has42 = /meaning\s*=\s*42/.test(content);
123+
buildCount++;
124+
switch (phase) {
126125
case 1:
127-
expect(content).toMatch(/meaning\s*=\s*42/);
128-
expect(content).not.toMatch(/meaning\s*=\s*10/);
129-
host.writeMultipleFiles({
130-
'src/meaning-too.ts': 'export var meaning = 84;',
131-
});
126+
const has10 = /meaning\s*=\s*10/.test(content);
127+
128+
if (has42 && !has10) {
129+
phase = 2;
130+
host.writeMultipleFiles({
131+
'src/meaning-too.ts': 'export var meaning = 84;',
132+
});
133+
}
132134
break;
133135

134136
case 2:
135-
expect(content).toMatch(/meaning\s*=\s*84/);
136-
expect(content).not.toMatch(/meaning\s*=\s*42/);
137+
const has84 = /meaning\s*=\s*84/.test(content);
138+
139+
if (has84 && !has42) {
140+
phase = 3;
141+
} else {
142+
// try triggering a rebuild again
143+
host.writeMultipleFiles({
144+
'src/meaning-too.ts': 'export var meaning = 84;',
145+
});
146+
}
137147
break;
138148
}
139149
}),
140-
take(2),
141-
).toPromise().then(() => done(), done.fail);
150+
takeWhile(() => phase < 3),
151+
).toPromise().then(
152+
() => done(),
153+
() => done.fail(`stuck at phase ${phase} [builds: ${buildCount}]`),
154+
);
142155
});
143156

144157
});

0 commit comments

Comments
 (0)