From 0b58a2263ff11fb181a6a103d4603c13a4d97496 Mon Sep 17 00:00:00 2001 From: Yangshun Date: Tue, 27 Oct 2026 13:17:35 +0800 Subject: [PATCH] test: isolate Astro dev playground --- packages/void/test/integration/astro.test.ts | 59 +++++++------------- 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/packages/void/test/integration/astro.test.ts b/packages/void/test/integration/astro.test.ts index 6d49d2007..629c348e7 100644 --- a/packages/void/test/integration/astro.test.ts +++ b/packages/void/test/integration/astro.test.ts @@ -28,37 +28,17 @@ function readWranglerConfig(root: string): { } describe('playground/astro dev', () => { - let child: ReturnType; - - // Dev writes Void's trigger middleware into src/ and removes it when the server stops. taskkill - // on Windows skips the exit hooks, and a later copy of this playground would keep a middleware - // that checks this server's trigger token. - const removeLeftoverMiddleware = () => { - const root = join(ROOT, PLAYGROUND); - const middleware = join(root, 'src/middleware.ts'); - if ( - !existsSync(middleware) || - !readFileSync(middleware, 'utf8').includes('Auto-generated by Void in dev') - ) { - return; - } - // The dev server's exit hook can remove it between the read and cleanup. - rmSync(middleware, { force: true }); - const gitignore = join(root, '.gitignore'); - writeFileSync( - gitignore, - readFileSync(gitignore, 'utf8').replace(/^src\/middleware\.ts\r?\n/m, ''), - ); - }; + const sourceGitignorePath = join(ROOT, PLAYGROUND, '.gitignore'); + const sourceGitignore = readFileSync(sourceGitignorePath, 'utf8'); + let playground: IsolatedPlayground; + let child: ReturnType | undefined; const startDev = async () => { const server = await startServer((port) => { child = spawn( - 'vp', + process.execPath, [ - 'run', - '--filter', - `./${PLAYGROUND}`, + join(playground.root, 'node_modules/astro/bin/astro.mjs'), 'dev', '--host', '127.0.0.1', @@ -66,11 +46,12 @@ describe('playground/astro dev', () => { String(port), ], { - cwd: ROOT, + cwd: playground.root, stdio: ['ignore', 'pipe', 'pipe'], env: { ...process.env, ASTRO_DEV_BACKGROUND: 'false', + ASTRO_DISABLE_UPDATE_CHECK: 'true', VOID_INSPECTOR_PORT: 'false', }, }, @@ -81,28 +62,30 @@ describe('playground/astro dev', () => { }; beforeAll(async () => { - const pg = join(ROOT, PLAYGROUND); - rmSync(join(pg, '.void'), { recursive: true, force: true }); - rmSync(join(pg, '.wrangler'), { recursive: true, force: true }); - - // Dev must generate the inferred binding config itself in a fresh CI checkout. + playground = createIsolatedPlayground(ROOT, PLAYGROUND); + // Dev must generate the inferred binding config itself in a fresh fixture. + rmSync(join(playground.root, '.void-wrangler.jsonc'), { force: true }); await startDev(); }); afterAll(async () => { - if (child) { - await killChild(child); + try { + if (child) { + await killChild(child); + } + } finally { + playground?.cleanup(); + expect(readFileSync(sourceGitignorePath, 'utf8')).toBe(sourceGitignore); } - removeLeftoverMiddleware(); }); it('syncs wrangler config with compat flag and inferred bindings', () => { - const wranglerConfig = readWranglerConfig(join(ROOT, PLAYGROUND)); + const wranglerConfig = readWranglerConfig(playground.root); expect(wranglerConfig.compatibility_flags?.includes('nodejs_als')).toBe(true); expect(wranglerConfig.d1_databases?.some((b) => b.binding === 'DB')).toBe(true); expect(wranglerConfig.kv_namespaces?.some((b) => b.binding === 'KV')).toBe(true); - expect(existsSync(join(ROOT, `${PLAYGROUND}/wrangler.jsonc`))).toBe(false); + expect(existsSync(join(playground.root, 'wrangler.jsonc'))).toBe(false); }); it('renders home page with SSR', async () => { @@ -131,7 +114,7 @@ describe('playground/astro dev', () => { // An app without middleware gets Void's dev middleware, which answers the trigger. it('runs a cron through the dev trigger endpoint', async () => { - const token = readFileSync(join(ROOT, PLAYGROUND, '.void/dev-trigger-token'), 'utf8').trim(); + const token = readFileSync(join(playground.root, '.void/dev-trigger-token'), 'utf8').trim(); const response = await fetch(`${DEV_BASE}/__void/scheduled`, { method: 'POST', headers: { 'x-void-dev-trigger': token },