82 lines
1.7 KiB
TypeScript
82 lines
1.7 KiB
TypeScript
import { spawn } from 'node:child_process';
|
|
import { Socket } from 'node:net';
|
|
|
|
const host = process.env.PLAYWRIGHT_HOST ?? '127.0.0.1';
|
|
const requestedPort = Number(process.env.PLAYWRIGHT_PORT ?? '3400');
|
|
const candidatePorts = [
|
|
requestedPort,
|
|
3401,
|
|
3402,
|
|
3410,
|
|
3500,
|
|
3600,
|
|
3700,
|
|
3800
|
|
];
|
|
|
|
async function isPortAvailable(port: number) {
|
|
return await new Promise<boolean>((resolve) => {
|
|
const socket = new Socket();
|
|
|
|
const finish = (available: boolean) => {
|
|
socket.destroy();
|
|
resolve(available);
|
|
};
|
|
|
|
socket.setTimeout(250);
|
|
socket.once('connect', () => finish(false));
|
|
socket.once('timeout', () => finish(false));
|
|
socket.once('error', (error: NodeJS.ErrnoException) => {
|
|
if (error.code === 'ECONNREFUSED' || error.code === 'EHOSTUNREACH') {
|
|
finish(true);
|
|
return;
|
|
}
|
|
|
|
finish(false);
|
|
});
|
|
|
|
try {
|
|
socket.connect(port, host);
|
|
} catch {
|
|
finish(false);
|
|
}
|
|
});
|
|
}
|
|
|
|
async function pickPort() {
|
|
for (const port of candidatePorts) {
|
|
if (await isPortAvailable(port)) {
|
|
return port;
|
|
}
|
|
}
|
|
|
|
throw new Error(`Unable to find an open Playwright port from: ${candidatePorts.join(', ')}`);
|
|
}
|
|
|
|
const port = await pickPort();
|
|
const baseURL = `http://${host}:${port}`;
|
|
console.info(`[e2e] using ${baseURL}`);
|
|
|
|
const child = spawn(
|
|
'bun',
|
|
['x', 'playwright', 'test', ...process.argv.slice(2)],
|
|
{
|
|
stdio: 'inherit',
|
|
env: {
|
|
...process.env,
|
|
PLAYWRIGHT_BASE_URL: baseURL,
|
|
PLAYWRIGHT_HOST: host,
|
|
PLAYWRIGHT_PORT: String(port)
|
|
}
|
|
}
|
|
);
|
|
|
|
child.on('exit', (code, signal) => {
|
|
if (signal) {
|
|
process.exit(signal === 'SIGINT' || signal === 'SIGTERM' ? 0 : 1);
|
|
return;
|
|
}
|
|
|
|
process.exit(code ?? 0);
|
|
});
|