Skip to content

Commit c06031e

Browse files
committed
test: use random port for e2e express http server tests
1 parent be4ea9e commit c06031e

7 files changed

+53
-19
lines changed

tests/legacy-cli/e2e/tests/i18n/ivy-localize-basehref.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ export default async function () {
5959
await ng('e2e', `--configuration=${lang}`, '--port=0');
6060

6161
// Execute Application E2E tests for a production build without dev server
62-
const server = externalServer(outputPath, (baseHrefs[lang] as string) || '/');
62+
const { server, port, url } = await externalServer(
63+
outputPath,
64+
(baseHrefs[lang] as string) || '/',
65+
);
6366
try {
6467
await ng(
6568
'e2e',
69+
`--port=${port}`,
6670
`--configuration=${lang}`,
6771
'--dev-server-target=',
68-
`--base-url=http://localhost:4200${baseHrefs[lang] || '/'}`,
72+
`--base-url=${url}`,
6973
);
7074
} finally {
7175
server.close();
@@ -89,13 +93,17 @@ export default async function () {
8993
await ng('e2e', `--configuration=${lang}`, '--port=0');
9094

9195
// Execute Application E2E tests for a production build without dev server
92-
const server = externalServer(outputPath, '/test' + (baseHrefs[lang] || '/'));
96+
const { server, port, url } = await externalServer(
97+
outputPath,
98+
'/test' + (baseHrefs[lang] || '/'),
99+
);
93100
try {
94101
await ng(
95102
'e2e',
103+
`--port=${port}`,
96104
`--configuration=${lang}`,
97105
'--dev-server-target=',
98-
`--base-url=http://localhost:4200/test${baseHrefs[lang] || '/'}`,
106+
`--base-url=${url}`,
99107
);
100108
} finally {
101109
server.close();

tests/legacy-cli/e2e/tests/i18n/ivy-localize-es2015.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ export default async function () {
3636
await ng('e2e', `--configuration=${lang}`, '--port=0');
3737

3838
// Execute Application E2E tests for a production build without dev server
39-
const server = externalServer(outputPath, `/${lang}/`);
39+
const { server, port, url } = await externalServer(outputPath, `/${lang}/`);
4040
try {
4141
await ng(
4242
'e2e',
43+
`--port=${port}`,
4344
`--configuration=${lang}`,
4445
'--dev-server-target=',
45-
`--base-url=http://localhost:4200/${lang}/`,
46+
`--base-url=${url}`,
4647
);
4748
} finally {
4849
server.close();

tests/legacy-cli/e2e/tests/i18n/ivy-localize-es5.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ export default async function () {
4444
await ng('e2e', `--configuration=${lang}`, '--port=0');
4545

4646
// Execute Application E2E tests for a production build without dev server
47-
const server = externalServer(outputPath, `/${lang}/`);
47+
const { server, port, url } = await externalServer(outputPath, `/${lang}/`);
4848
try {
4949
await ng(
5050
'e2e',
51+
`--port=${port}`,
5152
`--configuration=${lang}`,
5253
'--dev-server-target=',
53-
`--base-url=http://localhost:4200/${lang}/`,
54+
`--base-url=${url}`,
5455
);
5556
} finally {
5657
server.close();

tests/legacy-cli/e2e/tests/i18n/ivy-localize-locale-data-augment.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ export default async function () {
4343
await ng('e2e', `--configuration=${lang}`, '--port=0');
4444

4545
// Execute Application E2E tests for a production build without dev server
46-
const server = externalServer(outputPath, `/${lang}/`);
46+
const { server, port, url } = await externalServer(outputPath, `/${lang}/`);
4747
try {
4848
await ng(
4949
'e2e',
50+
`--port=${port}`,
5051
`--configuration=${lang}`,
5152
'--dev-server-target=',
52-
`--base-url=http://localhost:4200/${lang}/`,
53+
`--base-url=${url}`,
5354
);
5455
} finally {
5556
server.close();

tests/legacy-cli/e2e/tests/i18n/ivy-localize-server.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express from 'express';
22
import { join } from 'path';
33
import { getGlobalVariable } from '../../utils/env';
44
import { appendToFile, expectFileToMatch, writeFile } from '../../utils/fs';
5+
import { findFreePort } from '../../utils/network';
56
import { installWorkspacePackages } from '../../utils/packages';
67
import { ng } from '../../utils/process';
78
import { updateJsonFile } from '../../utils/project';
@@ -13,6 +14,7 @@ const snapshots = require('../../ng-snapshot/package.json');
1314
export default async function () {
1415
// TODO: Re-enable pending further Ivy/Universal/i18n work
1516
return;
17+
const port = await findFreePort();
1618

1719
// Setup i18n tests and config.
1820
await setupI18nConfig();
@@ -113,10 +115,10 @@ export default async function () {
113115
const { i18nApp } = (await import(serverBundle)) as {
114116
i18nApp(locale: string): express.Express;
115117
};
116-
const server = i18nApp(lang).listen(4200, 'localhost');
118+
const server = i18nApp(lang).listen(port, 'localhost');
117119
try {
118120
// Execute without a devserver.
119-
await ng('e2e', `--configuration=${lang}`, '--dev-server-target=');
121+
await ng('e2e', `--port=${port}`, `--configuration=${lang}`, '--dev-server-target=');
120122
} finally {
121123
server.close();
122124
}

tests/legacy-cli/e2e/tests/i18n/ivy-localize-serviceworker.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
replaceInFile,
99
writeFile,
1010
} from '../../utils/fs';
11+
import { findFreePort } from '../../utils/network';
1112
import { installPackage } from '../../utils/packages';
1213
import { ng } from '../../utils/process';
1314
import { updateJsonFile } from '../../utils/project';
@@ -18,6 +19,7 @@ export default async function () {
1819
// TEMP: disable pending i18n updates
1920
// TODO: when re-enabling, use setupI18nConfig and helpers like other i18n tests.
2021
return;
22+
const port = await findFreePort();
2123

2224
let localizeVersion = '@angular/localize@' + readNgVersion();
2325
if (getGlobalVariable('argv')['ng-snapshots']) {
@@ -156,7 +158,7 @@ export default async function () {
156158
// Ivy i18n doesn't yet work with `ng serve` so we must use a separate server.
157159
const app = express();
158160
app.use(express.static(resolve(baseDir, lang)));
159-
const server = app.listen(4200, 'localhost');
161+
const server = app.listen(port, 'localhost');
160162
try {
161163
// Add E2E test for locale
162164
await writeFile(
@@ -180,7 +182,7 @@ export default async function () {
180182
);
181183

182184
// Execute without a devserver.
183-
await ng('e2e', '--dev-server-target=');
185+
await ng('e2e', '--dev-server-target=', `--port=${port}`);
184186
} finally {
185187
server.close();
186188
}

tests/legacy-cli/e2e/tests/i18n/setup.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import {
99
replaceInFile,
1010
writeFile,
1111
} from '../../utils/fs';
12+
import { findFreePort } from '../../utils/network';
1213
import { installPackage } from '../../utils/packages';
1314
import { ng } from '../../utils/process';
1415
import { updateJsonFile } from '../../utils/project';
15-
import { expectToFail } from '../../utils/utils';
1616
import { readNgVersion } from '../../utils/version';
17+
import { Server } from 'http';
1718

1819
// Configurations for each locale.
1920
export const baseDir = 'dist/test-project';
@@ -67,13 +68,31 @@ export const langTranslations = [
6768
];
6869
export const sourceLocale = langTranslations[0].lang;
6970

70-
export const externalServer = (outputPath: string, baseUrl = '/') => {
71+
export interface ExternalServer {
72+
readonly server: Server;
73+
readonly port: number;
74+
readonly url: string;
75+
}
76+
77+
/**
78+
* Create an `express` `http.Server` listening on a random port.
79+
*
80+
* Call .close() on the server return value to close the server.
81+
*/
82+
export async function externalServer(outputPath: string, baseUrl = '/'): Promise<ExternalServer> {
83+
const port = await findFreePort();
84+
7185
const app = express();
7286
app.use(baseUrl, express.static(resolve(outputPath)));
7387

74-
// call .close() on the return value to close the server.
75-
return app.listen(4200, 'localhost');
76-
};
88+
const server = app.listen(port, 'localhost');
89+
90+
return {
91+
server,
92+
port,
93+
url: `http://localhost:${port}${baseUrl}`,
94+
};
95+
}
7796

7897
export const formats = {
7998
'xlf': {

0 commit comments

Comments
 (0)