Skip to content
Open
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
6 changes: 6 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ app or directory that a test writes from scratch with `createPackageFixtureDir()
`packageFixturePath()`, not inside a test directory or a package root: Vitest lists those
directories, and an entry that comes and goes there reruns every cached test task.

Start every server that a test stops later, such as `vp dev`, `vp preview` or a built app, with
`spawnFixtureServer()` or `spawnTestServer()` rather than `spawn`. They run the server under
`test/server-supervisor.ts`, which stops its whole process tree when the test process exits, also
when the task runner or Vitest kills it with SIGKILL. A server left running would keep `vp run test`
waiting for it: every Vite+ process in a task's tree holds the task runner's IPC channel open.

For a leaf-module edit with known import-based consumers, Vitest's native related command is useful:

```sh
Expand Down
16 changes: 10 additions & 6 deletions packages/md/test/integration/invalid-frontmatter.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import spawn from 'cross-spawn';
import type spawn from 'cross-spawn';
import { afterAll, beforeAll, describe, expect, it } from 'vite-plus/test';
import {
createIsolatedPlayground,
killChild,
spawnTestServer,
startServer,
} from '../../../../test/integration-helpers.ts';
import type { IsolatedPlayground } from '../../../../test/integration-helpers.ts';
Expand Down Expand Up @@ -38,11 +39,14 @@ describe('invalid frontmatter on the dev server', () => {
}

const server = await startServer((port) => {
child = spawn('vp', ['dev', '--host', '127.0.0.1', '--port', String(port), '--strictPort'], {
cwd: playground.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, VOID_INSPECTOR_PORT: 'false' },
});
child = spawnTestServer(
'vp',
['dev', '--host', '127.0.0.1', '--port', String(port), '--strictPort'],
{
cwd: playground.root,
env: { ...process.env, VOID_INSPECTOR_PORT: 'false' },
},
);
return child;
}, 30_000);
base = server.base;
Expand Down
13 changes: 6 additions & 7 deletions packages/void/test/integration/astro.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import type { AddressInfo } from 'node:net';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { PGlite } from '@electric-sql/pglite';
import spawn from 'cross-spawn';
import type spawn from 'cross-spawn';
import { createServer } from 'pglite-server';
import { afterAll, beforeAll, describe, expect, it } from 'vite-plus/test';
import {
createIsolatedPlayground,
killChild,
spawnTestServer,
startServer,
} from '../../../../test/integration-helpers.ts';
import type { IsolatedPlayground, StartedServer } from '../../../../test/integration-helpers.ts';
Expand Down Expand Up @@ -47,7 +48,7 @@ describe('playground/astro dev', () => {

const startDev = async () => {
const server = await startServer((port) => {
child = spawn(
child = spawnTestServer(
process.execPath,
[
join(playground.root, 'node_modules/astro/bin/astro.mjs'),
Expand All @@ -59,7 +60,6 @@ describe('playground/astro dev', () => {
],
{
cwd: playground.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: {
...process.env,
ASTRO_DEV_BACKGROUND: 'false',
Expand Down Expand Up @@ -172,7 +172,7 @@ export const onRequest = defineMiddleware((_context, next) => next());
function start(script: 'dev' | 'preview'): Promise<StartedServer> {
const root = playground.root;
return startServer((port) => {
child = spawn(
child = spawnTestServer(
process.execPath,
[
join(root, 'node_modules/astro/bin/astro.mjs'),
Expand All @@ -184,7 +184,6 @@ export const onRequest = defineMiddleware((_context, next) => next());
],
{
cwd: root,
stdio: ['ignore', 'pipe', 'pipe'],
env: {
...process.env,
ASTRO_DEV_BACKGROUND: 'false',
Expand Down Expand Up @@ -478,10 +477,10 @@ export const GET = () =>
it('serves .env values from the Worker env and public values as Vite defines them', async () => {
const server = await startServer(
(port) =>
spawn(
spawnTestServer(
process.execPath,
[astro(), 'preview', '--host', '127.0.0.1', '--port', String(port)],
{ cwd: playground.root, stdio: ['ignore', 'pipe', 'pipe'], env: env() },
{ cwd: playground.root, env: env() },
),
20_000,
);
Expand Down
16 changes: 10 additions & 6 deletions packages/void/test/integration/auth-schema-startup.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { execFileSync } from 'node:child_process';
import { mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import spawn from 'cross-spawn';
import type spawn from 'cross-spawn';
import { afterEach, expect, it } from 'vite-plus/test';
import {
createIsolatedPlayground,
killChild,
spawnTestServer,
startServer,
} from '../../../../test/integration-helpers.ts';
import type { IsolatedPlayground } from '../../../../test/integration-helpers.ts';
Expand Down Expand Up @@ -119,11 +120,14 @@ it('starts a fresh Cloudflare app with auth table imports without running prepar
freshAuthApp();
const server = await startServer(
(port) => {
child = spawn('vp', ['dev', '--host', '127.0.0.1', '--port', String(port), '--strictPort'], {
cwd: fixture.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, VOID_INSPECTOR_PORT: 'false' },
});
child = spawnTestServer(
'vp',
['dev', '--host', '127.0.0.1', '--port', String(port), '--strictPort'],
{
cwd: fixture.root,
env: { ...process.env, VOID_INSPECTOR_PORT: 'false' },
},
);
return child;
},
30_000,
Expand Down
15 changes: 9 additions & 6 deletions packages/void/test/integration/http-errors-playground-suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from
import {
createIsolatedPlayground,
killChild,
spawnTestServer,
startServer,
} from '../../../../test/integration-helpers.ts';
import type { IsolatedPlayground, StartedServer } from '../../../../test/integration-helpers.ts';
Expand Down Expand Up @@ -150,13 +151,15 @@ function httpErrorsSuite(mode: HttpErrorsMode): void {
server = await startServer(
(port) =>
mode === 'dev'
? spawn('vp', ['dev', '--host', '127.0.0.1', '--port', String(port), '--strictPort'], {
? spawnTestServer(
'vp',
['dev', '--host', '127.0.0.1', '--port', String(port), '--strictPort'],
{
cwd: playground.root,
},
)
: spawnTestServer(process.execPath, ['dist/ssr/index.js'], {
cwd: playground.root,
stdio: ['ignore', 'pipe', 'pipe'],
})
: spawn(process.execPath, ['dist/ssr/index.js'], {
cwd: playground.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, PORT: String(port), NODE_ENV: 'production' },
}),
30_000,
Expand Down
15 changes: 9 additions & 6 deletions packages/void/test/integration/node-target.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { afterAll, beforeAll, describe, expect, it } from 'vite-plus/test';
import {
createIsolatedPlayground,
killChild,
spawnTestServer,
startServer,
} from '../../../../test/integration-helpers.ts';
import type { IsolatedPlayground } from '../../../../test/integration-helpers.ts';
Expand All @@ -23,10 +24,13 @@ describe('playground/node-app dev', () => {
beforeAll(async () => {
playground = createIsolatedPlayground(ROOT, 'playground/node-app');
const server = await startServer((port) => {
child = spawn('vp', ['dev', '--host', '127.0.0.1', '--port', String(port), '--strictPort'], {
cwd: playground.root,
stdio: ['ignore', 'pipe', 'pipe'],
});
child = spawnTestServer(
'vp',
['dev', '--host', '127.0.0.1', '--port', String(port), '--strictPort'],
{
cwd: playground.root,
},
);
return child;
}, 20_000);
DEV_BASE = server.base;
Expand Down Expand Up @@ -93,9 +97,8 @@ describe('playground/node-app build + production', () => {
expect(build.status, `${build.stdout ?? ''}${build.stderr ?? ''}`).toBe(0);

const server = await startServer((port) => {
child = spawn('node', ['dist/ssr/index.js'], {
child = spawnTestServer('node', ['dist/ssr/index.js'], {
cwd: playground.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, PORT: String(port) },
});
return child;
Expand Down
20 changes: 12 additions & 8 deletions packages/void/test/integration/nuxt.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { existsSync, readFileSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import spawn from 'cross-spawn';
import type spawn from 'cross-spawn';
import { afterAll, beforeAll, describe, expect, it } from 'vite-plus/test';
import { collectBrowserDiagnostics } from '../../../../test/browser-helpers.ts';
import {
createIsolatedPlayground,
killChild,
spawnTestServer,
startServer,
} from '../../../../test/integration-helpers.ts';
import type { IsolatedPlayground } from '../../../../test/integration-helpers.ts';
Expand Down Expand Up @@ -37,14 +38,17 @@ describe(
// URL under Vitest, so only the page body shows that the answer comes from this app.
const server = await startServer(
(port) => {
child = spawn('vp', ['run', 'dev', '--host', '127.0.0.1', '--port', String(port)], {
cwd: fixture.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: {
...process.env,
VOID_INSPECTOR_PORT: 'false',
child = spawnTestServer(
'vp',
['run', 'dev', '--host', '127.0.0.1', '--port', String(port)],
{
cwd: fixture.root,
env: {
...process.env,
VOID_INSPECTOR_PORT: 'false',
},
},
});
);
return child;
},
30_000,
Expand Down
12 changes: 5 additions & 7 deletions packages/void/test/integration/page-errors-suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import {
readFileSync,
} from 'node:fs';
import { join } from 'node:path';
import spawn from 'cross-spawn';
import type spawn from 'cross-spawn';
import type { Browser } from 'playwright';
import { afterAll, beforeAll, describe, expect, it } from 'vite-plus/test';
import { collectBrowserDiagnostics } from '../../../../test/browser-helpers.ts';
import {
createIsolatedPlayground,
killChild,
spawnTestServer,
startServer,
} from '../../../../test/integration-helpers.ts';
import type { IsolatedPlayground } from '../../../../test/integration-helpers.ts';
Expand Down Expand Up @@ -276,9 +277,8 @@ export const loader = defineHandler(c => {
});
const server = await startServer(
(port) =>
(child = spawn(process.execPath, ['dist/ssr/index.js'], {
(child = spawnTestServer(process.execPath, ['dist/ssr/index.js'], {
cwd: fixture.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, PORT: String(port) },
})),
30_000,
Expand Down Expand Up @@ -604,12 +604,11 @@ export const loader = defineHandler(c => {
write('public/download', 'Download bytes');
const dev = await startServer(
(port) =>
(child = spawn(
(child = spawnTestServer(
'vp',
['dev', '--host', '127.0.0.1', '--port', String(port), '--strictPort'],
{
cwd: fixture.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, NODE_ENV: 'development' },
},
)),
Expand Down Expand Up @@ -646,12 +645,11 @@ export const loader = defineHandler(c => {
);
const trailing = await startServer(
(port) =>
(child = spawn(
(child = spawnTestServer(
'vp',
['dev', '--host', '127.0.0.1', '--port', String(port), '--strictPort'],
{
cwd: fixture.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, NODE_ENV: 'development' },
},
)),
Expand Down
10 changes: 4 additions & 6 deletions packages/void/test/integration/pages-target-assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { afterAll, beforeAll, describe, expect, it } from 'vite-plus/test';
import {
createIsolatedPlayground,
killChild,
spawnTestServer,
startServer,
} from '../../../../test/integration-helpers.ts';
import type { IsolatedPlayground } from '../../../../test/integration-helpers.ts';
Expand Down Expand Up @@ -77,9 +78,8 @@ export default function Home() {
writeFileSync(join(root, 'public/health.txt'), 'public asset');
execFileSync('vp', ['build'], { cwd: root, stdio: 'pipe' });
const server = await startServer((port) => {
child = spawn(command, [...args, 'dist/ssr/index.js'], {
child = spawnTestServer(command, [...args, 'dist/ssr/index.js'], {
cwd: root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, PORT: String(port) },
});
return child;
Expand Down Expand Up @@ -132,9 +132,8 @@ export default function Home() {
mkdirSync(elsewhere);
const relocated = await startServer(
(port) =>
spawn(command, [...args, join(root, 'dist/ssr/index.js')], {
spawnTestServer(command, [...args, join(root, 'dist/ssr/index.js')], {
cwd: elsewhere,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, PORT: String(port) },
}),
20_000,
Expand Down Expand Up @@ -196,9 +195,8 @@ ${serve}
try {
const server = await startServer(
(port) =>
spawn(command, [...args, 'supplied-server.mjs'], {
spawnTestServer(command, [...args, 'supplied-server.mjs'], {
cwd: root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, PORT: String(port) },
}),
20_000,
Expand Down
29 changes: 18 additions & 11 deletions packages/void/test/integration/react-router.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { execFileSync } from 'node:child_process';
import { existsSync, globSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import spawn from 'cross-spawn';
import type spawn from 'cross-spawn';
import { afterAll, beforeAll, describe, expect, it, onTestFinished } from 'vite-plus/test';
import {
createIsolatedPlayground,
killChild,
spawnFixtureServer,
spawnTestServer,
startServer,
} from '../../../../test/integration-helpers.ts';
import type { IsolatedPlayground, StartedServer } from '../../../../test/integration-helpers.ts';
Expand Down Expand Up @@ -156,11 +157,14 @@ export default {
}
const server = await startServer(
(port) =>
spawn('vp', [script, '--host', '127.0.0.1', '--port', String(port), '--strictPort'], {
cwd: fixture.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, VOID_INSPECTOR_PORT: 'false' },
}),
spawnTestServer(
'vp',
[script, '--host', '127.0.0.1', '--port', String(port), '--strictPort'],
{
cwd: fixture.root,
env: { ...process.env, VOID_INSPECTOR_PORT: 'false' },
},
),
30_000,
);
onTestFinished(() => killChild(server.child));
Expand Down Expand Up @@ -266,11 +270,14 @@ export async function loader() {

function start(script: 'dev' | 'preview'): Promise<StartedServer> {
return startServer((port) => {
child = spawn('vp', [script, '--host', '127.0.0.1', '--port', String(port), '--strictPort'], {
cwd: playground.root,
stdio: ['ignore', 'pipe', 'pipe'],
env: { ...process.env, VOID_INSPECTOR_PORT: 'false' },
});
child = spawnTestServer(
'vp',
[script, '--host', '127.0.0.1', '--port', String(port), '--strictPort'],
{
cwd: playground.root,
env: { ...process.env, VOID_INSPECTOR_PORT: 'false' },
},
);
return child;
}, 30_000);
}
Expand Down
Loading