Skip to content

Commit 237715f

Browse files
committed
test(@angular-devkit/build-angular): improve reliability of app-shell server-worker test
The test now waits until the express server is listening and uses a try/finally block to ensure the server is shutdown. The test server also only now binds to localhost and lets the OS choose a port. This provides better support for running multiple tests in parallel.
1 parent d3d2cc7 commit 237715f

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

packages/angular_devkit/build_angular/src/app-shell/app-shell_spec.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import { Architect } from '@angular-devkit/architect';
1010
import { getSystemPath, join, normalize, virtualFs } from '@angular-devkit/core';
1111
import * as express from 'express'; // tslint:disable-line:no-implicit-dependencies
12+
import * as http from 'http';
13+
import { AddressInfo } from 'net';
1214
import { createArchitect, host } from '../test-utils';
1315

1416
describe('AppShell Builder', () => {
@@ -251,19 +253,25 @@ describe('AppShell Builder', () => {
251253
// Serve the app using a simple static server.
252254
const app = express();
253255
app.use('/', express.static(getSystemPath(join(host.root(), 'dist')) + '/'));
254-
const server = app.listen(4200);
255-
256-
// Load app in protractor, then check service worker status.
257-
const protractorRun = await architect.scheduleTarget(
258-
{ project: 'app-e2e', target: 'e2e' },
259-
{ devServerTarget: undefined } as {},
260-
);
261-
const protractorOutput = await protractorRun.result;
262-
await protractorRun.stop();
263-
expect(protractorOutput.success).toBe(true);
264-
265-
// Close the express server.
266-
server.close();
256+
const server = await new Promise<http.Server>((resolve) => {
257+
const innerServer = app.listen(0, 'localhost', () => resolve(innerServer));
258+
});
259+
try {
260+
const serverPort = (server.address() as AddressInfo).port;
261+
// Load app in protractor, then check service worker status.
262+
const protractorRun = await architect.scheduleTarget(
263+
{ project: 'app-e2e', target: 'e2e' },
264+
{ baseUrl: `http://localhost:${serverPort}/`, devServerTarget: '' },
265+
);
266+
267+
const protractorOutput = await protractorRun.result;
268+
await protractorRun.stop();
269+
270+
expect(protractorOutput.success).toBe(true);
271+
} finally {
272+
// Close the express server.
273+
await new Promise<void>((resolve) => server.close(() => resolve()));
274+
}
267275
});
268276

269277
it('critical CSS is inlined', async () => {

0 commit comments

Comments
 (0)