Skip to content
Merged
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
59 changes: 21 additions & 38 deletions packages/void/test/integration/astro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,49 +28,30 @@ function readWranglerConfig(root: string): {
}

describe('playground/astro dev', () => {
let child: ReturnType<typeof spawn>;

// 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<typeof spawn> | 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',
'--port',
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',
},
},
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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 },
Expand Down