Skip to content

Commit 2fc6f1a

Browse files
committed
test(@angular-devkit/build-ng-packagr): add tests for watch mode
1 parent 8689481 commit 2fc6f1a

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

packages/angular_devkit/build_ng_packagr/src/build/index_spec_large.ts

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88

99
import { TargetSpecifier } from '@angular-devkit/architect';
1010
import { TestProjectHost, runTargetSpec } from '@angular-devkit/architect/testing';
11-
import { join, normalize } from '@angular-devkit/core';
12-
import { tap } from 'rxjs/operators';
13-
11+
import { join, normalize, virtualFs } from '@angular-devkit/core';
12+
import { debounceTime, map, take, tap } from 'rxjs/operators';
1413

1514
const devkitRoot = normalize((global as any)._DevKitRoot); // tslint:disable-line:no-any
1615
const workspaceRoot = join(devkitRoot, 'tests/@angular_devkit/build_ng_packagr/ng-packaged/');
@@ -48,4 +47,59 @@ describe('NgPackagr Builder', () => {
4847
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
4948
).toPromise().then(done, done.fail);
5049
}, Timeout.Basic);
50+
51+
it('rebuilds on TS file changes', (done) => {
52+
const targetSpec: TargetSpecifier = { project: 'lib', target: 'build' };
53+
54+
const goldenValueFiles: { [path: string]: string } = {
55+
'projects/lib/src/lib/lib.component.ts': `
56+
import { Component } from '@angular/core';
57+
58+
@Component({
59+
selector: 'lib',
60+
template: 'lib update works!'
61+
})
62+
export class LibComponent { }
63+
`,
64+
};
65+
66+
const overrides = { watch: true };
67+
68+
let buildNumber = 0;
69+
70+
runTargetSpec(host, targetSpec, overrides)
71+
.pipe(
72+
// We must debounce on watch mode because file watchers are not very accurate.
73+
// Changes from just before a process runs can be picked up and cause rebuilds.
74+
// In this case, cleanup from the test right before this one causes a few rebuilds.
75+
debounceTime(1000),
76+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
77+
map(() => {
78+
const fileName = './dist/lib/fesm5/lib.js';
79+
const content = virtualFs.fileBufferToString(
80+
host.scopedSync().read(normalize(fileName)),
81+
);
82+
83+
return content;
84+
}),
85+
tap(content => {
86+
buildNumber += 1;
87+
switch (buildNumber) {
88+
case 1:
89+
expect(content).toMatch(/lib works/);
90+
host.writeMultipleFiles(goldenValueFiles);
91+
break;
92+
93+
case 2:
94+
expect(content).toMatch(/lib update works/);
95+
break;
96+
default:
97+
break;
98+
}
99+
}),
100+
take(2),
101+
)
102+
.toPromise()
103+
.then(done, done.fail);
104+
}, Timeout.Standard);
51105
});

0 commit comments

Comments
 (0)