Skip to content

Commit c8240d9

Browse files
alan-agius4hansl
authored andcommitted
test(@angular-devkit/build-ng-packagr): add tests for watch mode
1 parent 50fc721 commit c8240d9

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

+57-3
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/');
@@ -43,4 +42,59 @@ describe('NgPackagr Builder', () => {
4342
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
4443
).toPromise().then(done, done.fail);
4544
});
45+
46+
it('rebuilds on TS file changes', (done) => {
47+
const targetSpec: TargetSpecifier = { project: 'lib', target: 'build' };
48+
49+
const goldenValueFiles: { [path: string]: string } = {
50+
'projects/lib/src/lib/lib.component.ts': `
51+
import { Component } from '@angular/core';
52+
53+
@Component({
54+
selector: 'lib',
55+
template: 'lib update works!'
56+
})
57+
export class LibComponent { }
58+
`,
59+
};
60+
61+
const overrides = { watch: true };
62+
63+
let buildNumber = 0;
64+
65+
runTargetSpec(host, targetSpec, overrides)
66+
.pipe(
67+
// We must debounce on watch mode because file watchers are not very accurate.
68+
// Changes from just before a process runs can be picked up and cause rebuilds.
69+
// In this case, cleanup from the test right before this one causes a few rebuilds.
70+
debounceTime(1000),
71+
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
72+
map(() => {
73+
const fileName = './dist/lib/fesm5/lib.js';
74+
const content = virtualFs.fileBufferToString(
75+
host.scopedSync().read(normalize(fileName)),
76+
);
77+
78+
return content;
79+
}),
80+
tap(content => {
81+
buildNumber += 1;
82+
switch (buildNumber) {
83+
case 1:
84+
expect(content).toMatch(/lib works/);
85+
host.writeMultipleFiles(goldenValueFiles);
86+
break;
87+
88+
case 2:
89+
expect(content).toMatch(/lib update works/);
90+
break;
91+
default:
92+
break;
93+
}
94+
}),
95+
take(2),
96+
)
97+
.toPromise()
98+
.then(done, done.fail);
99+
});
46100
});

0 commit comments

Comments
 (0)