diff --git a/frontend/tests/setup-backend.ts b/frontend/tests/setup-backend.ts new file mode 100644 index 00000000..6acf7aa4 --- /dev/null +++ b/frontend/tests/setup-backend.ts @@ -0,0 +1,39 @@ +import { spawn } from 'child_process'; +import { ChildProcess } from 'node:child_process'; + +let backendProcess: ChildProcess; + +export async function setup(): Promise { + // Spin up the database + spawn('docker', ['compose', 'up', 'db', '--detach'], { + cwd: '..', + stdio: "pipe", + }); + + backendProcess = spawn('npm', ['run', 'dev'], { + cwd: '../backend', + stdio: "pipe", + }); + + // 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 + } + } +} + +export async function teardown(): Promise { + if (backendProcess) { + backendProcess.kill(); + } + + spawn('docker', ['compose', 'down'], { + cwd: '..', + stdio: "pipe" + }); +} diff --git a/frontend/vitest.config.ts b/frontend/vitest.config.ts index ba2d72b6..2a75f180 100644 --- a/frontend/vitest.config.ts +++ b/frontend/vitest.config.ts @@ -9,6 +9,8 @@ export default mergeConfig( environment: "jsdom", exclude: [...configDefaults.exclude, "e2e/**"], root: fileURLToPath(new URL("./", import.meta.url)), + // Startup the backend server, because it is needed for some tests + globalSetup: [ "./tests/setup-backend.ts" ] }, }), );