Skip to content

Commit a51eb6e

Browse files
clydinfilipesilva
authored andcommitted
test(@angular-devkit/build-angular): add dev-server execute and fetch unit test helper
This change extracts common test code into a helper function to reduce complexity of the dev-server builder unit tests.
1 parent 06a354f commit a51eb6e

File tree

4 files changed

+59
-61
lines changed

4 files changed

+59
-61
lines changed

packages/angular_devkit/build_angular/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ ts_library(
266266
"//packages/angular_devkit/architect/testing",
267267
"//packages/angular_devkit/core",
268268
"//packages/angular_devkit/core/node",
269+
"@npm//@types/node-fetch",
269270
"@npm//rxjs",
270271
],
271272
)

packages/angular_devkit/build_angular/src/dev-server/tests/behavior/build-inline-critical-css_spec.ts

+4-16
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import fetch from 'node-fetch'; // tslint:disable-line:no-implicit-dependencies
9-
import { mergeMap, take, timeout } from 'rxjs/operators';
108
import { serveWebpackBrowser } from '../../index';
9+
import { executeOnceAndFetch } from '../execute-fetch';
1110
import {
1211
BASE_OPTIONS,
1312
DEV_SERVER_BUILDER_INFO,
@@ -41,21 +40,10 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
4140
...BASE_OPTIONS,
4241
});
4342

44-
await harness
45-
.execute()
46-
.pipe(
47-
timeout(39000),
48-
mergeMap(async ({ result }) => {
49-
expect(result?.success).toBeTrue();
43+
const { result, response } = await executeOnceAndFetch(harness, '/');
5044

51-
if (result?.success) {
52-
const response = await fetch(`${result.baseUrl}index.html`);
53-
expect(await response.text()).toContain(`body{color:#000;}`);
54-
}
55-
}),
56-
take(1),
57-
)
58-
.toPromise();
45+
expect(result?.success).toBeTrue();
46+
expect(await response?.text()).toContain('body{color:#000;}');
5947
});
6048
});
6149
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 fetch, { RequestInit, Response } from 'node-fetch'; // tslint:disable-line:no-implicit-dependencies
9+
import { mergeMap, take, timeout } from 'rxjs/operators';
10+
import { URL } from 'url';
11+
import {
12+
BuilderHarness,
13+
BuilderHarnessExecutionOptions,
14+
BuilderHarnessExecutionResult,
15+
} from '../../testing/builder-harness';
16+
17+
export async function executeOnceAndFetch<T>(
18+
harness: BuilderHarness<T>,
19+
url: string,
20+
options?: Partial<BuilderHarnessExecutionOptions> & { request?: RequestInit },
21+
): Promise<BuilderHarnessExecutionResult & { response?: Response }> {
22+
return harness
23+
.execute()
24+
.pipe(
25+
timeout(30000),
26+
mergeMap(async (executionResult) => {
27+
let response = undefined;
28+
if (executionResult.result?.success) {
29+
const resolvedUrl = new URL(url, executionResult.result.baseUrl as string);
30+
response = await fetch(resolvedUrl, options?.request);
31+
}
32+
33+
return { ...executionResult, response };
34+
}),
35+
take(1),
36+
)
37+
.toPromise();
38+
}

packages/angular_devkit/build_angular/src/dev-server/tests/options/allowed-hosts_spec.ts

+16-45
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import fetch from 'node-fetch'; // tslint:disable-line:no-implicit-dependencies
9-
import { mergeMap, take, timeout } from 'rxjs/operators';
108
import { serveWebpackBrowser } from '../../index';
9+
import { executeOnceAndFetch } from '../execute-fetch';
1110
import {
1211
BASE_OPTIONS,
13-
BUILD_TIMEOUT,
1412
DEV_SERVER_BUILDER_INFO,
1513
describeBuilder,
1614
setupBrowserTarget,
@@ -32,21 +30,12 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
3230
...BASE_OPTIONS,
3331
});
3432

35-
await harness
36-
.execute()
37-
.pipe(
38-
timeout(BUILD_TIMEOUT),
39-
mergeMap(async ({ result }) => {
40-
expect(result?.success).toBeTrue();
33+
const { result, response } = await executeOnceAndFetch(harness, '/', {
34+
request: { headers: FETCH_HEADERS },
35+
});
4136

42-
if (result?.success) {
43-
const response = await fetch(`${result.baseUrl}`, { headers: FETCH_HEADERS });
44-
expect(await response.text()).toBe('Invalid Host header');
45-
}
46-
}),
47-
take(1),
48-
)
49-
.toPromise();
37+
expect(result?.success).toBeTrue();
38+
expect(await response?.text()).toBe('Invalid Host header');
5039
});
5140

5241
it('does not allow an invalid host when option is an empty array', async () => {
@@ -55,21 +44,12 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
5544
allowedHosts: [],
5645
});
5746

58-
await harness
59-
.execute()
60-
.pipe(
61-
timeout(BUILD_TIMEOUT),
62-
mergeMap(async ({ result }) => {
63-
expect(result?.success).toBeTrue();
47+
const { result, response } = await executeOnceAndFetch(harness, '/', {
48+
request: { headers: FETCH_HEADERS },
49+
});
6450

65-
if (result?.success) {
66-
const response = await fetch(`${result.baseUrl}`, { headers: FETCH_HEADERS });
67-
expect(await response.text()).toBe('Invalid Host header');
68-
}
69-
}),
70-
take(1),
71-
)
72-
.toPromise();
51+
expect(result?.success).toBeTrue();
52+
expect(await response?.text()).toBe('Invalid Host header');
7353
});
7454

7555
it('allows a host when specified in the option', async () => {
@@ -78,21 +58,12 @@ describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
7858
allowedHosts: ['example.com'],
7959
});
8060

81-
await harness
82-
.execute()
83-
.pipe(
84-
timeout(BUILD_TIMEOUT),
85-
mergeMap(async ({ result }) => {
86-
expect(result?.success).toBeTrue();
61+
const { result, response } = await executeOnceAndFetch(harness, '/', {
62+
request: { headers: FETCH_HEADERS },
63+
});
8764

88-
if (result?.success) {
89-
const response = await fetch(`${result.baseUrl}`, { headers: FETCH_HEADERS });
90-
expect(await response.text()).toContain('<title>');
91-
}
92-
}),
93-
take(1),
94-
)
95-
.toPromise();
65+
expect(result?.success).toBeTrue();
66+
expect(await response?.text()).toContain('<title>');
9667
});
9768
});
9869
});

0 commit comments

Comments
 (0)