Skip to content

Commit

Permalink
test: Astro Node API instead of node:child_process
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Sep 25, 2023
1 parent 0df75ee commit dca057a
Showing 1 changed file with 20 additions and 39 deletions.
59 changes: 20 additions & 39 deletions packages/site/test/setup.ts
Original file line number Diff line number Diff line change
@@ -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<ReturnType<typeof preview>>;

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<typeof console.log>) {
Expand Down

0 comments on commit dca057a

Please sign in to comment.