Skip to content

Commit

Permalink
test: use random port for e2e express http server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Jun 9, 2022
1 parent be4ea9e commit c06031e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
16 changes: 12 additions & 4 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-basehref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ export default async function () {
await ng('e2e', `--configuration=${lang}`, '--port=0');

// Execute Application E2E tests for a production build without dev server
const server = externalServer(outputPath, (baseHrefs[lang] as string) || '/');
const { server, port, url } = await externalServer(
outputPath,
(baseHrefs[lang] as string) || '/',
);
try {
await ng(
'e2e',
`--port=${port}`,
`--configuration=${lang}`,
'--dev-server-target=',
`--base-url=http://localhost:4200${baseHrefs[lang] || '/'}`,
`--base-url=${url}`,
);
} finally {
server.close();
Expand All @@ -89,13 +93,17 @@ export default async function () {
await ng('e2e', `--configuration=${lang}`, '--port=0');

// Execute Application E2E tests for a production build without dev server
const server = externalServer(outputPath, '/test' + (baseHrefs[lang] || '/'));
const { server, port, url } = await externalServer(
outputPath,
'/test' + (baseHrefs[lang] || '/'),
);
try {
await ng(
'e2e',
`--port=${port}`,
`--configuration=${lang}`,
'--dev-server-target=',
`--base-url=http://localhost:4200/test${baseHrefs[lang] || '/'}`,
`--base-url=${url}`,
);
} finally {
server.close();
Expand Down
5 changes: 3 additions & 2 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ export default async function () {
await ng('e2e', `--configuration=${lang}`, '--port=0');

// Execute Application E2E tests for a production build without dev server
const server = externalServer(outputPath, `/${lang}/`);
const { server, port, url } = await externalServer(outputPath, `/${lang}/`);
try {
await ng(
'e2e',
`--port=${port}`,
`--configuration=${lang}`,
'--dev-server-target=',
`--base-url=http://localhost:4200/${lang}/`,
`--base-url=${url}`,
);
} finally {
server.close();
Expand Down
5 changes: 3 additions & 2 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-es5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ export default async function () {
await ng('e2e', `--configuration=${lang}`, '--port=0');

// Execute Application E2E tests for a production build without dev server
const server = externalServer(outputPath, `/${lang}/`);
const { server, port, url } = await externalServer(outputPath, `/${lang}/`);
try {
await ng(
'e2e',
`--port=${port}`,
`--configuration=${lang}`,
'--dev-server-target=',
`--base-url=http://localhost:4200/${lang}/`,
`--base-url=${url}`,
);
} finally {
server.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ export default async function () {
await ng('e2e', `--configuration=${lang}`, '--port=0');

// Execute Application E2E tests for a production build without dev server
const server = externalServer(outputPath, `/${lang}/`);
const { server, port, url } = await externalServer(outputPath, `/${lang}/`);
try {
await ng(
'e2e',
`--port=${port}`,
`--configuration=${lang}`,
'--dev-server-target=',
`--base-url=http://localhost:4200/${lang}/`,
`--base-url=${url}`,
);
} finally {
server.close();
Expand Down
6 changes: 4 additions & 2 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from 'express';
import { join } from 'path';
import { getGlobalVariable } from '../../utils/env';
import { appendToFile, expectFileToMatch, writeFile } from '../../utils/fs';
import { findFreePort } from '../../utils/network';
import { installWorkspacePackages } from '../../utils/packages';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
Expand All @@ -13,6 +14,7 @@ const snapshots = require('../../ng-snapshot/package.json');
export default async function () {
// TODO: Re-enable pending further Ivy/Universal/i18n work
return;
const port = await findFreePort();

// Setup i18n tests and config.
await setupI18nConfig();
Expand Down Expand Up @@ -113,10 +115,10 @@ export default async function () {
const { i18nApp } = (await import(serverBundle)) as {
i18nApp(locale: string): express.Express;
};
const server = i18nApp(lang).listen(4200, 'localhost');
const server = i18nApp(lang).listen(port, 'localhost');
try {
// Execute without a devserver.
await ng('e2e', `--configuration=${lang}`, '--dev-server-target=');
await ng('e2e', `--port=${port}`, `--configuration=${lang}`, '--dev-server-target=');
} finally {
server.close();
}
Expand Down
6 changes: 4 additions & 2 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-serviceworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
replaceInFile,
writeFile,
} from '../../utils/fs';
import { findFreePort } from '../../utils/network';
import { installPackage } from '../../utils/packages';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
Expand All @@ -18,6 +19,7 @@ export default async function () {
// TEMP: disable pending i18n updates
// TODO: when re-enabling, use setupI18nConfig and helpers like other i18n tests.
return;
const port = await findFreePort();

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

// Execute without a devserver.
await ng('e2e', '--dev-server-target=');
await ng('e2e', '--dev-server-target=', `--port=${port}`);
} finally {
server.close();
}
Expand Down
29 changes: 24 additions & 5 deletions tests/legacy-cli/e2e/tests/i18n/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {
replaceInFile,
writeFile,
} from '../../utils/fs';
import { findFreePort } from '../../utils/network';
import { installPackage } from '../../utils/packages';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
import { expectToFail } from '../../utils/utils';
import { readNgVersion } from '../../utils/version';
import { Server } from 'http';

// Configurations for each locale.
export const baseDir = 'dist/test-project';
Expand Down Expand Up @@ -67,13 +68,31 @@ export const langTranslations = [
];
export const sourceLocale = langTranslations[0].lang;

export const externalServer = (outputPath: string, baseUrl = '/') => {
export interface ExternalServer {
readonly server: Server;
readonly port: number;
readonly url: string;
}

/**
* Create an `express` `http.Server` listening on a random port.
*
* Call .close() on the server return value to close the server.
*/
export async function externalServer(outputPath: string, baseUrl = '/'): Promise<ExternalServer> {
const port = await findFreePort();

const app = express();
app.use(baseUrl, express.static(resolve(outputPath)));

// call .close() on the return value to close the server.
return app.listen(4200, 'localhost');
};
const server = app.listen(port, 'localhost');

return {
server,
port,
url: `http://localhost:${port}${baseUrl}`,
};
}

export const formats = {
'xlf': {
Expand Down

0 comments on commit c06031e

Please sign in to comment.