Skip to content

Commit a6d886d

Browse files
clydinalan-agius4
authored andcommitted
test(@angular-devkit/build-angular): use port 0 in dev-server builder tests
This change updates the remainder of the non-builder harness tests to use a random port when testing. This change reduces the frequency of test flakes as well as improves the opportunity for parallel test execution.
1 parent 119256f commit a6d886d

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

packages/angular_devkit/build_angular/src/dev-server/index_spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ describe('Dev Server Builder index', () => {
2525
});
2626

2727
const architect = (await createArchitect(host.root())).architect;
28-
const run = await architect.scheduleTarget(targetSpec);
28+
const run = await architect.scheduleTarget(targetSpec, { port: 0 });
2929
const output = (await run.result) as DevServerBuilderOutput;
3030
expect(output.success).toBe(true);
31-
const response = await fetch('http://localhost:4200/index.html');
31+
const response = await fetch(output.baseUrl);
3232
expect(await response.text()).toContain(
3333
'<script src="runtime.js" type="module"></script>' +
3434
'<script src="polyfills.js" type="module"></script>' +
@@ -67,10 +67,10 @@ describe('Dev Server Builder index', () => {
6767
await workspaces.writeWorkspace(workspace, workspaces.createWorkspaceHost(host));
6868

6969
const architect = (await createArchitect(host.root())).architect;
70-
const run = await architect.scheduleTarget(targetSpec);
70+
const run = await architect.scheduleTarget(targetSpec, { port: 0 });
7171
const output = (await run.result) as DevServerBuilderOutput;
7272
expect(output.success).toBe(true);
73-
const response = await fetch('http://localhost:4200/index.html');
73+
const response = await fetch(output.baseUrl);
7474
expect(await response.text()).toContain(
7575
'<script src="runtime.js" type="module"></script>' +
7676
'<script src="polyfills.js" type="module"></script>' +
@@ -89,10 +89,10 @@ describe('Dev Server Builder index', () => {
8989
});
9090

9191
const architect = (await createArchitect(host.root())).architect;
92-
const run = await architect.scheduleTarget(targetSpec);
92+
const run = await architect.scheduleTarget(targetSpec, { port: 0 });
9393
const output = (await run.result) as DevServerBuilderOutput;
9494
expect(output.success).toBe(true);
95-
const response = await fetch('http://localhost:4200/index.html');
95+
const response = await fetch(output.baseUrl);
9696
expect(await response.text()).toContain(
9797
'<script src="runtime.js" defer></script>' +
9898
'<script src="polyfills.js" defer></script>' +
@@ -132,10 +132,10 @@ describe('Dev Server Builder index', () => {
132132
await workspaces.writeWorkspace(workspace, workspaces.createWorkspaceHost(host));
133133

134134
const architect = (await createArchitect(host.root())).architect;
135-
const run = await architect.scheduleTarget(targetSpec);
135+
const run = await architect.scheduleTarget(targetSpec, { port: 0 });
136136
const output = (await run.result) as DevServerBuilderOutput;
137137
expect(output.success).toBe(true);
138-
const response = await fetch('http://localhost:4200/index.html');
138+
const response = await fetch(output.baseUrl);
139139
expect(await response.text()).toContain(`lang="${locale}"`);
140140
await run.stop();
141141
});

packages/angular_devkit/build_angular/src/dev-server/ssl_spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ describe('Dev Server Builder ssl', () => {
3030
});
3131

3232
it('works', async () => {
33-
const run = await architect.scheduleTarget(target, { ssl: true });
33+
const run = await architect.scheduleTarget(target, { ssl: true, port: 0 });
3434
runs.push(run);
3535
const output = await run.result as DevServerBuilderOutput;
3636
expect(output.success).toBe(true);
37-
expect(output.baseUrl).toBe('https://localhost:4200/');
37+
expect(output.baseUrl).toMatch(/^https:\/\/localhost:\d+\//);
3838

39-
const response = await fetch('https://localhost:4200/index.html', {
39+
const response = await fetch(output.baseUrl, {
4040
agent: new https.Agent({ rejectUnauthorized: false }),
4141
});
4242
expect(await response.text()).toContain('<title>HelloWorldApp</title>');
@@ -104,15 +104,16 @@ describe('Dev Server Builder ssl', () => {
104104
ssl: true,
105105
sslKey: 'ssl/server.key',
106106
sslCert: 'ssl/server.crt',
107+
port: 0,
107108
};
108109

109110
const run = await architect.scheduleTarget(target, overrides);
110111
runs.push(run);
111112
const output = await run.result as DevServerBuilderOutput;
112113
expect(output.success).toBe(true);
113-
expect(output.baseUrl).toBe('https://localhost:4200/');
114+
expect(output.baseUrl).toMatch(/^https:\/\/localhost:\d+\//);
114115

115-
const response = await fetch('https://localhost:4200/index.html', {
116+
const response = await fetch(output.baseUrl, {
116117
agent: new https.Agent({ rejectUnauthorized: false }),
117118
});
118119
expect(await response.text()).toContain('<title>HelloWorldApp</title>');

packages/angular_devkit/build_angular/src/dev-server/works_spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('Dev Server Builder', () => {
4747

4848
it('should not generate sourcemaps when running prod build', async () => {
4949
// Production builds have sourcemaps turned off.
50-
const run = await architect.scheduleTarget({ ...target, configuration: 'production' });
50+
const run = await architect.scheduleTarget({ ...target, configuration: 'production' }, { port: 0 });
5151
runs.push(run);
5252
const output = await run.result as DevServerBuilderOutput;
5353
expect(output.success).toBe(true);
@@ -57,11 +57,11 @@ describe('Dev Server Builder', () => {
5757

5858
it('serves custom headers', async () => {
5959
const run = await architect.scheduleTarget(
60-
target, {headers: {'X-Header': 'Hello World'}});
60+
target, {headers: {'X-Header': 'Hello World'}, port: 0});
6161
runs.push(run);
6262
const output = await run.result as DevServerBuilderOutput;
6363
expect(output.success).toBe(true);
64-
const response = await fetch('http://localhost:4200/index.html');
64+
const response = await fetch(output.baseUrl);
6565
expect(response.headers.get('X-Header')).toBe('Hello World');
6666
});
6767

@@ -77,13 +77,13 @@ describe('Dev Server Builder', () => {
7777
});
7878

7979
const architect = (await createArchitect(host.root())).architect;
80-
const run = await architect.scheduleTarget(target);
81-
const output = await run.result;
80+
const run = await architect.scheduleTarget(target, { port: 0 });
81+
const output = await run.result as DevServerBuilderOutput;
8282
expect(output.success).toBe(true);
8383

84-
const indexResponse = await fetch('http://localhost:4200/index.html');
84+
const indexResponse = await fetch(output.baseUrl);
8585
expect(await indexResponse.text()).toContain('lang="fr"');
86-
const vendorResponse = await fetch('http://localhost:4200/vendor.js');
86+
const vendorResponse = await fetch(output.baseUrl + 'vendor.js');
8787
const vendorText = await vendorResponse.text();
8888
expect(vendorText).toContain('fr');
8989
expect(vendorText).toContain('octobre');

0 commit comments

Comments
 (0)