Use a dynamic port for the test-http-api to avoid conflicts

Pick a free port at config load time using a TCP probe on port 0, inject
it into the Vite dev server via VITE_TEST_HTTP_API_PORT, and have
globalSetup start the test-http-api on the same port. The frontend
reads the env var at build time instead of the previously hardcoded 8081.
This commit is contained in:
Rūdolfs Ošiņš
2026-03-23 10:59:39 -06:00
parent 293f6adb52
commit cf40e86a8e
4 changed files with 27 additions and 11 deletions
+17 -2
View File
@@ -1,7 +1,23 @@
import type { PlaywrightTestConfig } from "@playwright/test";
import * as net from "node:net";
import { devices } from "@playwright/test";
function getFreePort(): Promise<number> {
return new Promise((resolve, reject) => {
const server = net.createServer();
server.listen(0, "127.0.0.1", () => {
const port = (server.address() as net.AddressInfo).port;
server.close(err => (err ? reject(err) : resolve(port)));
});
server.on("error", reject);
});
}
const testHttpApiPort = await getFreePort();
process.env.VITE_TEST_HTTP_API_PORT = String(testHttpApiPort);
const config: PlaywrightTestConfig = {
outputDir: "./tests/artifacts",
testDir: "./tests/e2e",
@@ -33,8 +49,7 @@ const config: PlaywrightTestConfig = {
webServer: [
{
command:
"VITE_AUTH_LONG_DELAY=1000 npm run start -- --strictPort --port 3001",
command: `VITE_AUTH_LONG_DELAY=1000 VITE_TEST_HTTP_API_PORT=${testHttpApiPort} npm run start -- --strictPort --port 3001`,
port: 3001,
},
],
+8 -5
View File
@@ -49,11 +49,14 @@ async function withTestBackend<T>(
}
});
} else {
return fetch(`http://127.0.0.1:8081/${cmd}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(args),
}).then(async response => {
return fetch(
`http://127.0.0.1:${import.meta.env.VITE_TEST_HTTP_API_PORT ?? 8081}/${cmd}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(args),
},
).then(async response => {
if (response.status === 404) {
console.log("Got a 404 response:", response);
return null;
-1
View File
@@ -503,7 +503,6 @@ export const shortBobHead = formatOid(bobHead);
export const cobRid = "rad:z3fpY7nttPPa6MBnAv2DccHzQJnqe";
export const markdownRid = "rad:z2tchH2Ti4LxRKdssPQYs6VHE5rsg";
export const shortNodeRemote = "z6MktU…1xB22S";
export const defaultHttpdPort = 8081;
export const gitOptions = {
alice: {
GIT_AUTHOR_NAME: "Alice Liddell",
+2 -3
View File
@@ -5,7 +5,6 @@ import {
createCobsFixture,
createMarkdownFixture,
defaultConfig,
defaultHttpdPort,
gitOptions,
} from "@tests/support/fixtures.js";
import { createPeerManager } from "@tests/support/peerManager.js";
@@ -68,7 +67,7 @@ export default async function globalSetup(): Promise<() => void> {
alias: "palm",
},
});
await palm.startHttpd(defaultHttpdPort);
await palm.startHttpd(parseInt(process.env.VITE_TEST_HTTP_API_PORT ?? "0"));
try {
console.log("Creating markdown fixture");
@@ -87,7 +86,7 @@ export default async function globalSetup(): Promise<() => void> {
}
await palm.stopNode();
} else {
await palm.startHttpd(defaultHttpdPort);
await palm.startHttpd(parseInt(process.env.VITE_TEST_HTTP_API_PORT ?? "0"));
}
return async () => {