Skip to content

Commit c9bdfc7

Browse files
clydinfilipesilva
authored andcommitted
test(@angular-devkit/build-angular): add dev-server builder watch option tests
This change adds expanded unit tests for the dev-server builder's `watch` option using the builder test harness.
1 parent 55b833c commit c9bdfc7

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { TimeoutError } from 'rxjs';
9+
import { concatMap, count, take, timeout } from 'rxjs/operators';
10+
import { serveWebpackBrowser } from '../../index';
11+
import {
12+
BASE_OPTIONS,
13+
BUILD_TIMEOUT,
14+
DEV_SERVER_BUILDER_INFO,
15+
describeBuilder,
16+
setupBrowserTarget,
17+
} from '../setup';
18+
19+
describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
20+
describe('Option: "watch"', () => {
21+
beforeEach(() => {
22+
setupBrowserTarget(harness);
23+
});
24+
25+
it('does not wait for file changes when false', async () => {
26+
harness.useTarget('serve', {
27+
...BASE_OPTIONS,
28+
watch: false,
29+
});
30+
31+
await harness
32+
.execute()
33+
.pipe(
34+
timeout(BUILD_TIMEOUT * 2),
35+
concatMap(async ({ result }, index) => {
36+
expect(result?.success).toBe(true);
37+
38+
switch (index) {
39+
case 0:
40+
await harness.modifyFile(
41+
'src/main.ts',
42+
(content) => content + 'console.log("abcd1234");',
43+
);
44+
break;
45+
case 1:
46+
fail('Expected files to not be watched.');
47+
break;
48+
}
49+
}),
50+
take(2),
51+
)
52+
.toPromise()
53+
.catch((error) => {
54+
// Timeout is expected if watching is disabled
55+
if (error instanceof TimeoutError) {
56+
return;
57+
}
58+
throw error;
59+
});
60+
});
61+
62+
it('watches for file changes when not present', async () => {
63+
harness.useTarget('serve', {
64+
...BASE_OPTIONS,
65+
});
66+
67+
const buildCount = await harness
68+
.execute()
69+
.pipe(
70+
timeout(BUILD_TIMEOUT * 2),
71+
concatMap(async ({ result }, index) => {
72+
expect(result?.success).toBe(true);
73+
74+
switch (index) {
75+
case 0:
76+
await harness.modifyFile(
77+
'src/main.ts',
78+
(content) => content + 'console.log("abcd1234");',
79+
);
80+
break;
81+
case 1:
82+
break;
83+
}
84+
}),
85+
take(2),
86+
count(),
87+
)
88+
.toPromise();
89+
90+
expect(buildCount).toBe(2);
91+
});
92+
93+
it('watches for file changes when true', async () => {
94+
harness.useTarget('serve', {
95+
...BASE_OPTIONS,
96+
watch: true,
97+
});
98+
99+
const buildCount = await harness
100+
.execute()
101+
.pipe(
102+
timeout(BUILD_TIMEOUT * 2),
103+
concatMap(async ({ result }, index) => {
104+
expect(result?.success).toBe(true);
105+
106+
switch (index) {
107+
case 0:
108+
await harness.modifyFile(
109+
'src/main.ts',
110+
(content) => content + 'console.log("abcd1234");',
111+
);
112+
break;
113+
case 1:
114+
break;
115+
}
116+
}),
117+
take(2),
118+
count(),
119+
)
120+
.toPromise();
121+
122+
expect(buildCount).toBe(2);
123+
});
124+
});
125+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { buildWebpackBrowser } from '../../browser';
9+
import { BASE_OPTIONS as BROWSER_BASE_OPTIONS } from '../../browser/tests/setup';
10+
import { BuilderHarness } from '../../testing/builder-harness';
11+
import { Schema } from '../schema';
12+
13+
export { describeBuilder } from '../../testing';
14+
15+
export const DEV_SERVER_BUILDER_INFO = Object.freeze({
16+
name: '@angular-devkit/build-angular:dev-server',
17+
schemaPath: __dirname + '/../schema.json',
18+
});
19+
20+
/**
21+
* Contains all required extract-i18n builder fields.
22+
* The port is also set to zero to ensure a free port is used for each test which
23+
* supports parallel test execution.
24+
*/
25+
export const BASE_OPTIONS = Object.freeze<Schema>({
26+
browserTarget: 'test:build',
27+
port: 0,
28+
});
29+
30+
/**
31+
* Maximum time for single build/rebuild
32+
* This accounts for CI variability.
33+
*/
34+
export const BUILD_TIMEOUT = 15000;
35+
36+
export function setupBrowserTarget<T>(harness: BuilderHarness<T>): void {
37+
harness.withBuilderTarget('build', buildWebpackBrowser, {
38+
...BROWSER_BASE_OPTIONS,
39+
});
40+
}

0 commit comments

Comments
 (0)