Skip to content
Draft
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
9 changes: 9 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ share a scheduling phase. `vp run test:unit` includes `unit`, `unit-pglite`, and
The cf runner's transport checks live in `cf-version-transport.test.ts` so real subprocesses do
not serialize the whole runner suite. Both files use the same fixture helpers with per-file
isolated roots and cleanup; keep their real HTTP, process, and deadline assertions.
The cf runner, transport, build-output, and operation-process files run their real-subprocess cases
concurrently within each file (`const it = serialIt.concurrent`). A case that uses `vi` mocks or
spies must use `serialIt`: Vitest clears mock history whenever a test starts, so a concurrent
neighbor would erase the calls being asserted. Resolve fixture output through `outputRoot(prepared)`
instead of a shared "latest root" variable.
Browser suites with independent variants (`page-errors-*`, `server-auth-errors-*`,
`http-errors-playground-*`) keep their cases in a `*-suite.ts` module and expose one thin test file
per variant, so the variants spread across workers. Add a new variant as another thin file and give
it an estimate in `test/integration-timings.ts`.
Unit files and most API integration files use isolated worker threads to reduce startup time. On Linux and macOS,
API integration files and the PGlite unit files (`unit-pglite`, `platform-unit-pglite`) use forks to isolate PGlite's
WebAssembly JIT allocations after native Node 24 crashes in shared worker processes. API tests
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"access": "public"
},
"scripts": {
"build": "vp pack && node scripts/build-runtime.mjs && vp run build:stamp",
"build": "vp pack && vp run build:runtime && vp run build:stamp",
"dev": "vp pack --watch",
"typecheck": "tsc --noEmit"
},
Expand Down
9 changes: 9 additions & 0 deletions packages/platform/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { defineConfig } from 'vite-plus';
export default defineConfig({
run: {
tasks: {
// The middle step of the `build` script. It empties dist/runtime before writing it, which
// the task cache records as reading those files. `build:stamp` then edits the manifest in
// that directory, so without this exclusion the runtime build would never be a cache hit.
'build:runtime': {
command: 'node scripts/build-runtime.mjs',
cache: {
input: [{ auto: true }, '!dist/runtime/**'],
},
},
// The last step of the `build` script: records the source revision in the runtime manifest.
// It isn't cached, so a new revision reruns only this step, and it gets the full
// environment, including VOID_PLATFORM_SOURCE_REVISION.
Expand Down
28 changes: 24 additions & 4 deletions packages/void/test/helpers/cf-version-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import type { AddressInfo } from 'node:net';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterEach, expect, vi } from 'vite-plus/test';
import { afterAll, afterEach, expect, vi } from 'vite-plus/test';
import { prepareCfBuildOutput } from '../../src/cli/cf-build-output.ts';
import { createCfCommand } from '../../src/cli/cf-version-runner.ts';

// Vitest's per-file module isolation gives each suite its own roots and hooks.
const roots: Array<string> = [];
const outputRoots = new WeakMap<object, string>();
const cleanups: Array<() => unknown> = [];
const pinnedCfVersion = JSON.parse(
readFileSync(fileURLToPath(import.meta.resolve('cf/package.json')), 'utf8'),
Expand All @@ -23,9 +24,17 @@ const cfUserAgent: string = `cf-cli/${pinnedCfVersion} void/${voidVersion}`;
const REAL_CF_TIMEOUT_MS: number = process.platform === 'win32' ? 20_000 : 15_000;
const REAL_CF_ACTIVATION_TIMEOUT_MS: number = process.platform === 'win32' ? 60_000 : 30_000;

afterEach(async () => {
vi.restoreAllMocks();
syncBuiltinESMExports();
afterEach(({ task }) => {
// Only serial tests patch globals. Restoring after a concurrent test would also reset the
// local mocks of the tests still running beside it.
if (!task.concurrent) {
vi.restoreAllMocks();
syncBuiltinESMExports();
}
});
// Concurrent tests share these lists, so one test's end must not release another test's
// servers and directories. Everything is released when the file ends.
afterAll(async () => {
for (const cleanup of cleanups.splice(0).reverse()) {
await cleanup();
}
Expand Down Expand Up @@ -114,15 +123,26 @@ async function output(
);
const prepared = await prepareCfBuildOutput(worker, 'account-id');
expect(prepared).not.toBeNull();
outputRoots.set(prepared!, root);
return prepared!;
}

/** The project root that `output()` created for this prepared output. */
function outputRoot(prepared: object): string {
const root = outputRoots.get(prepared);
if (!root) {
throw new Error('This output was not created by output().');
}
return root;
}

export {
cfUserAgent,
cleanups,
expectSameJson,
listen,
output,
outputRoot,
pinnedCf,
pinnedCfVersion,
REAL_CF_ACTIVATION_TIMEOUT_MS,
Expand Down
10 changes: 7 additions & 3 deletions packages/void/test/helpers/cloudflare-operation-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import type { CloudflareOperation } from '@void/deploy-cloudflare';
import { afterEach } from 'vite-plus/test';
import { afterAll } from 'vite-plus/test';
import { encodeCloudflareOperation } from '../../src/cli/cloudflare-process.ts';
import { resolveCloudflareOperationArgv } from '../../src/cli/preset.ts';

/** Cleanups run after each test, last registered first. Tests may push their own. */
/**
* Cleanups run when the file ends, last registered first. Tests may push their own.
* Tests that only wait for a child process run concurrently, so the end of one test must not
* release the servers and directories of another.
*/
export const cleanups: Array<() => unknown> = [];
afterEach(async () => {
afterAll(async () => {
for (const cleanup of cleanups.splice(0).reverse()) {
await cleanup();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { describeHttpErrorsPlayground } from './http-errors-playground-suite.ts';

describeHttpErrorsPlayground('dev');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { describeHttpErrorsPlayground } from './http-errors-playground-suite.ts';

describeHttpErrorsPlayground('production');
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ type PageData = {
deferredKeys?: Record<string, string>;
};

describe.each(['dev', 'production'] as const)('HTTP errors playground (%s)', (mode) => {
export type HttpErrorsMode = 'dev' | 'production';

/** Each mode runs from its own test file, so both servers and browser matrices run in parallel. */
export function describeHttpErrorsPlayground(mode: HttpErrorsMode): void {
describe(`HTTP errors playground (${mode})`, () => httpErrorsSuite(mode));
}

function httpErrorsSuite(mode: HttpErrorsMode): void {
let playground: IsolatedPlayground;
let server: StartedServer;
let browser: Browser;
Expand Down Expand Up @@ -367,4 +374,4 @@ describe.each(['dev', 'production'] as const)('HTTP errors playground (%s)', (mo
expect(await page.title()).toBe('HTTP Errors Example');
});
});
});
}
3 changes: 3 additions & 0 deletions packages/void/test/integration/page-errors-react.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { describePageErrors } from './page-errors-suite.ts';

describePageErrors('react');
3 changes: 3 additions & 0 deletions packages/void/test/integration/page-errors-solid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { describePageErrors } from './page-errors-suite.ts';

describePageErrors('solid');
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ import { PAGES_PROTOCOL_VERSION } from '../../../edge/src/pages-protocol.ts';
import { deserializePageData } from '../../src/pages/serialize.ts';

const repository = join(import.meta.dirname, '../../../..');
const frameworks = ['react', 'vue', 'svelte', 'solid'] as const;
export type PageErrorsFramework = 'react' | 'vue' | 'svelte' | 'solid';

describe.each(frameworks)('Pages errors with %s', (framework) => {
/**
* Each framework runs from its own test file, so the four fixtures build and serve in parallel
* workers instead of one after another.
*/
export function describePageErrors(framework: PageErrorsFramework): void {
describe(`Pages errors with ${framework}`, () => pageErrorsSuite(framework));
}

function pageErrorsSuite(framework: PageErrorsFramework): void {
let fixture: IsolatedPlayground;
let child: ReturnType<typeof spawn>;
let browser: Browser;
Expand Down Expand Up @@ -708,4 +716,4 @@ export const loader = defineHandler(c => {
expect(native404).not.toContain('type="module"');
}
});
});
}
3 changes: 3 additions & 0 deletions packages/void/test/integration/page-errors-svelte.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { describePageErrors } from './page-errors-suite.ts';

describePageErrors('svelte');
3 changes: 3 additions & 0 deletions packages/void/test/integration/page-errors-vue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { describePageErrors } from './page-errors-suite.ts';

describePageErrors('vue');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { describeServerAuthErrors } from './server-auth-errors-suite.ts';

describeServerAuthErrors('session');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { describeServerAuthErrors } from './server-auth-errors-suite.ts';

describeServerAuthErrors('signed in');
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { describeServerAuthErrors } from './server-auth-errors-suite.ts';

describeServerAuthErrors('signed out');
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,21 @@ async function expectRedirect(
await page.waitForURL(`${server.base}${destination}`);
}

describe.each([
{ state: 'signed out', signedIn: false },
{ state: 'signed in', signedIn: true },
])('server auth: $state', ({ signedIn }) => {
export type ServerAuthErrorsPart = 'signed out' | 'signed in' | 'session';

/**
* Each part runs from its own test file with its own dev server and database, so the three
* serial browser matrices run in parallel workers.
*/
export function describeServerAuthErrors(part: ServerAuthErrorsPart): void {
if (part === 'session') {
describe('demo session and navigation', sessionSuite);
} else {
describe(`server auth: ${part}`, () => authStateSuite(part === 'signed in'));
}
}

function authStateSuite(signedIn: boolean): void {
beforeEach(() => newPage(signedIn));

describe.each(visits)('%s', (visit) => {
Expand Down Expand Up @@ -268,9 +279,9 @@ describe.each([
expect(page.url()).toBe(`${server.base}/`);
await expectLayout(timeOrigin, true);
});
});
}

describe('demo session and navigation', () => {
function sessionSuite(): void {
beforeEach(() => newPage());

it.each(['throw-redirect', 'auth-redirect'])(
Expand Down Expand Up @@ -419,4 +430,4 @@ describe('demo session and navigation', () => {
})
.toBe(false);
});
});
}
20 changes: 14 additions & 6 deletions packages/void/test/unit/cf-build-output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from '@void/deploy-cloudflare';
import { toSlash } from 'pathslash';
import { createBuilder } from 'vite';
import { afterEach, expect, it, vi } from 'vite-plus/test';
import { afterAll, afterEach, expect, it as serialIt, vi } from 'vite-plus/test';
import {
type CfBuildOutput,
prepareCfFrameworkBuildOutput,
Expand All @@ -50,11 +50,19 @@ const roots: Array<string> = [];
const CF_BUILD_TIMEOUT_MS = process.platform === 'win32' ? 45_000 : 15_000;
afterEach(() => {
vi.unstubAllEnvs();
});
// Concurrent tests share this list, so directories are removed when the file ends.
afterAll(() => {
for (const root of roots.splice(0)) {
rmSync(root, { recursive: true, force: true });
}
});

// Most tests here wait for a real cf child process with their own directory and local API
// server, so they run concurrently. Tests that use `vi` run alone through serialIt: they patch
// globals, and Vitest clears every mock's call history whenever another test starts.
const it = serialIt.concurrent;

function worker(config: Record<string, unknown>): string {
const root = mkdtempSync(join(tmpdir(), 'void-cf-build-output-test-'));
roots.push(root);
Expand Down Expand Up @@ -418,7 +426,7 @@ it('returns only the public fields of the prepared output', async () => {
});
});

it.skipIf(process.platform === 'win32')(
serialIt.skipIf(process.platform === 'win32')(
'rejects a legacy asset symlink outside the asset directory',
async () => {
const output = worker({
Expand All @@ -439,13 +447,13 @@ it.skipIf(process.platform === 'win32')(
},
);

it.skipIf(process.platform === 'win32')('rejects symlinked native modules', async () => {
serialIt.skipIf(process.platform === 'win32')('rejects symlinked native modules', async () => {
const root = await nativeBuild();
symlinkSync('index.js', join(getWorkerBundleDir(root), 'alias.js'));
await expect(prepareCfBuildOutput(root)).rejects.toThrow('Symlinks are not permitted');
});

it.skipIf(process.platform === 'win32')('rejects symlinked native assets', async () => {
serialIt.skipIf(process.platform === 'win32')('rejects symlinked native assets', async () => {
const root = await nativeBuild();
const secret = join(root, 'secret.txt');
writeFileSync(secret, 'private');
Expand Down Expand Up @@ -853,7 +861,7 @@ it(
CF_BUILD_TIMEOUT_MS,
);

it(
serialIt(
'keeps local .env client values out of framework Worker output',
async () => {
const root = mkdtempSync(join(tmpdir(), 'void-cf-framework-env-test-'));
Expand Down Expand Up @@ -1316,7 +1324,7 @@ it('reads a framework config as JSONC and still rejects one that is not an objec
}
});

it.skipIf(process.platform === 'win32')(
serialIt.skipIf(process.platform === 'win32')(
'builds a named Sandbox image into cf prebuilt output and cleans up its local tag',
async () => {
const output = worker({
Expand Down
Loading
Loading