From dca057a542ad9ac9dc61df3cda55e72f97dd09a1 Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Mon, 25 Sep 2023 18:37:10 +0300 Subject: [PATCH] test: Astro Node API instead of `node:child_process` --- packages/site/test/setup.ts | 59 +++++++++++++------------------------ 1 file changed, 20 insertions(+), 39 deletions(-) diff --git a/packages/site/test/setup.ts b/packages/site/test/setup.ts index 23c1ffe..813fad1 100644 --- a/packages/site/test/setup.ts +++ b/packages/site/test/setup.ts @@ -1,51 +1,32 @@ -import { ChildProcess, exec, execSync } from 'node:child_process'; -import { resolve } from 'node:path'; -import { fileURLToPath } from 'node:url'; +import { build, preview } from 'astro'; -const root = resolve(fileURLToPath(import.meta.url), '../'); - -let subprocess: ChildProcess; +let server: Awaited>; export async function setup() { log('Building astro site'); - execSync('pnpm build', { cwd: root }); - - log('Starting astro server'); - subprocess = exec('pnpm preview', { cwd: root }); - - await new Promise((resolve, reject) => { - const timer = setTimeout( - () => reject(new Error('Timeout waiting for "pnpm preview"')), - 10_000, - ); - - subprocess.stdout?.on('data', (data: Buffer) => { - if (data.toString().includes('started in')) { - clearTimeout(timer); - resolve(null); - } - }); - - subprocess.stderr?.on('data', (data: Buffer) => { - reject(data.toString()); - }); - }); + await build({}); + + const timer = setTimeout( + () => new Error('Timeout waiting for Astro preview'), + 10_000, + ); + + log('Starting Astro server 🚀'); + server = await preview({}); + + clearTimeout(timer); } export async function teardown() { - subprocess.kill(); + const timer = setTimeout( + () => new Error('Timeout waiting for Astro server to stop'), + 10_000, + ); + log('Stopping astro server'); + await server.stop(); - await new Promise((resolve, reject) => { - const timer = setTimeout( - () => reject(new Error('Timeout waiting for "pnpm preview" to exit')), - 10_000, - ); - subprocess.on('exit', () => { - clearTimeout(timer); - resolve(null); - }); - }); + clearTimeout(timer); } function log(...messages: Parameters) {