Skip to content

Commit

Permalink
test: use Astro Node API instead of node:child_process (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Sep 25, 2023
1 parent 0a4f490 commit 93c2273
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 78 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -26,6 +26,6 @@
"prettier-plugin-astro": "^0.11.0",
"prettier-plugin-tailwindcss": "^0.5.3",
"typescript": "^5.1.6",
"vitest": "^0.33.0"
"vitest": "^0.34.5"
}
}
56 changes: 20 additions & 36 deletions packages/site/test/setup.ts
@@ -1,51 +1,35 @@
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 server: Awaited<ReturnType<typeof preview>>;

let subprocess: ChildProcess;
const __filename = fileURLToPath(import.meta.url);
const root = resolve(__filename, '../..');

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({ root });

const timer = setTimeout(() => {
throw new Error('Timeout waiting for Astro preview');
}, 10_000);

log('Starting Astro server 🚀');
server = await preview({ root });

clearTimeout(timer);
}

export async function teardown() {
subprocess.kill();
const timer = setTimeout(() => {
throw 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
111 changes: 70 additions & 41 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 93c2273

Please sign in to comment.