Files
cinny-desktop/tests/e2e/dev-server.mjs
T

37 lines
1.2 KiB
JavaScript

import { spawn } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
// electron/main.js loads this URL whenever the app is not packaged, so the E2E
// run needs the vite dev server up. It is the same server `npm run dev` uses.
export const DEV_URL = 'http://localhost:38347';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
const reachable = async () => {
try {
const res = await fetch(DEV_URL, { signal: AbortSignal.timeout(3000) });
return res.ok;
} catch {
return false;
}
};
export default async function globalSetup() {
// A dev server the developer already started is reused rather than restarted.
if (await reachable()) return () => {};
const child = spawn('npm', ['run', 'dev:vite'], { cwd: ROOT, detached: true, stdio: 'inherit' });
for (let attempt = 0; attempt < 60; attempt++) {
if (await reachable()) {
// detached puts npm and its sh -c child in their own process group.
return () => process.kill(-child.pid, 'SIGTERM');
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
process.kill(-child.pid, 'SIGTERM');
throw new Error(`Vite dev server did not come up at ${DEV_URL}`);
}