Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use Astro Node API instead of node:child_process #58

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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.