refactor(frontend): waitForEndpoint ipv loop

This commit is contained in:
Tibo De Peuter 2025-04-17 11:57:58 +02:00
parent 37be342137
commit 087215c4fd
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2

View file

@ -3,6 +3,17 @@ import { ChildProcess } from 'node:child_process';
let backendProcess: ChildProcess;
async function waitForEndpoint(url: string, delay = 1000): Promise<void> {
try {
await fetch(url);
} catch {
// Endpoint is not ready yet
await new Promise((resolve) => setTimeout(resolve, delay));
// Retry
await waitForEndpoint(url, delay);
}
}
export async function setup(): Promise<void> {
// Spin up the database
spawn('docker', ['compose', 'up', 'db', '--detach'], {
@ -16,15 +27,7 @@ export async function setup(): Promise<void> {
});
// Wait until you can curl the backend
let backendReady = false;
while (!backendReady) {
try {
await fetch('http://localhost:3000/api')
backendReady = true;
} catch (_) {
// Ignore the error
}
}
await waitForEndpoint('http://localhost:3000/api');
}
export async function teardown(): Promise<void> {