Skip to content

Commit

Permalink
test(@angular-devkit/build-angular): use port 0 in dev-server builder…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
clydin authored and alan-agius4 committed Apr 7, 2021
1 parent 119256f commit a6d886d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ describe('Dev Server Builder index', () => {
});

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

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

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

const architect = (await createArchitect(host.root())).architect;
const run = await architect.scheduleTarget(targetSpec);
const run = await architect.scheduleTarget(targetSpec, { port: 0 });
const output = (await run.result) as DevServerBuilderOutput;
expect(output.success).toBe(true);
const response = await fetch('http://localhost:4200/index.html');
const response = await fetch(output.baseUrl);
expect(await response.text()).toContain(`lang="${locale}"`);
await run.stop();
});
Expand Down
11 changes: 6 additions & 5 deletions packages/angular_devkit/build_angular/src/dev-server/ssl_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ describe('Dev Server Builder ssl', () => {
});

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

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

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

const response = await fetch('https://localhost:4200/index.html', {
const response = await fetch(output.baseUrl, {
agent: new https.Agent({ rejectUnauthorized: false }),
});
expect(await response.text()).toContain('<title>HelloWorldApp</title>');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('Dev Server Builder', () => {

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

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

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

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

const indexResponse = await fetch('http://localhost:4200/index.html');
const indexResponse = await fetch(output.baseUrl);
expect(await indexResponse.text()).toContain('lang="fr"');
const vendorResponse = await fetch('http://localhost:4200/vendor.js');
const vendorResponse = await fetch(output.baseUrl + 'vendor.js');
const vendorText = await vendorResponse.text();
expect(vendorText).toContain('fr');
expect(vendorText).toContain('octobre');
Expand Down

0 comments on commit a6d886d

Please sign in to comment.