// The unit, oracle, space and workerd suites. Fuzzers (test/fuzz/*.mjs) stay node scripts. test/workerd.test.js runs // the pipeline inside workerd (a devDependency) and is in CI's shards like any other file (#101). import { existsSync, readFileSync } from 'node:fs'; import { relative } from 'node:path'; import { fileURLToPath } from 'node:url'; import { BaseSequencer, type TestSpecification } from 'vitest/node'; import { defineConfig } from 'vitest/config'; // Source keeps `.js` specifiers for .ts files. vitest maps them itself; plain node does not, so every process the // suite starts (the forks below, worker_threads the space tests spawn, `node` child processes) inherits the resolve // hook through NODE_OPTIONS. Set before any pool starts, so the forks get it too (harmless there). const hook = `--import=${fileURLToPath(new URL('./test/ts-resolve.mjs', import.meta.url))}`; if (!(process.env['NODE_OPTIONS'] ?? '').includes(hook)) process.env['NODE_OPTIONS'] = [process.env['NODE_OPTIONS'], hook].filter(Boolean).join(' '); // Every git process the suite starts is test/git-shim/git (real git with background maintenance off; the reason is // in that file). PATH is set here, before any pool starts, because it is what every test hands on to git. const shim = fileURLToPath(new URL('./test/git-shim', import.meta.url)); if (!(process.env['PATH'] ?? '').split(':').includes(shim)) process.env['PATH'] = [shim, process.env['PATH']].filter(Boolean).join(':'); // `--shard i/N` by measured duration, not vitest's count-based split: longest-processing-time first over // test/durations.json (seconds per file on a CI runner: the four test-shard logs of run 42834161805 on 6f2dd469, 2026-11-10; // a file added since is its local time rounded up, until the next refresh). A file with no entry // weighs as the heaviest known file until it is measured. The plan is a pure function of the file list and the weights, // so the N shards of one commit cover every file exactly once. Within a shard, files start heaviest first. const WEIGHTS = 'test/durations.json'; const ROOT = fileURLToPath(new URL('.', import.meta.url)); const weights = (): Readonly> => (existsSync(WEIGHTS) ? JSON.parse(readFileSync(WEIGHTS, 'utf8')) : {}); const fileOf = (spec: TestSpecification): string => relative(ROOT, spec.moduleId).split('\\').join('/'); class WeightedSequencer extends BaseSequencer { override async shard(files: TestSpecification[]): Promise { const { index, count } = this.ctx.config.shard ?? { index: 1, count: 1 }; const known = weights(); const heaviest = Math.max(1, ...Object.values(known)); const weighted = files.map(spec => ({ spec, file: fileOf(spec), weight: known[fileOf(spec)] ?? heaviest })) .sort((a, b) => b.weight - a.weight || a.file.localeCompare(b.file)); const shards = Array.from({ length: count }, () => ({ specs: [] as TestSpecification[], weight: 0 })); for (const { spec, weight } of weighted) { let best = 0; for (let i = 1; i < count; i++) if (shards[i]!.weight < shards[best]!.weight) best = i; shards[best]!.specs.push(spec); shards[best]!.weight += weight; } return shards[index - 1]!.specs; } override async sort(files: TestSpecification[]): Promise { const known = weights(); return [...files].sort((a, b) => (known[fileOf(b)] ?? 0) - (known[fileOf(a)] ?? 0)); } } // Gate c (test/space/failsite-hook.mjs): with PLY_FAILSITES set, src/errors.ts is served instrumented to the test // files too, and each fork names its test file as the runner of what it records. Without it, nothing changes. const failsites = Boolean(process.env['PLY_FAILSITES']); const { ERRORS, instrument } = failsites ? await import('./test/space/failsite-instrument.mjs') : { ERRORS: '', instrument: (s: string): string => s }; // `npm run coverage`: test/coverage.mjs reads V8's counts against src/*.ts byte offsets, so the files run under node's // own import (type stripping keeps every offset) instead of vite's module runner, and each fork flushes its counts. const coverage = Boolean(process.env['NODE_V8_COVERAGE']); export default defineConfig({ plugins: failsites ? [{ name: 'ply-failsite', enforce: 'pre', // same line, columns after the insertion only: no source map needed for errors.ts (its own frames are not sites) transform: (code: string, id: string) => (id === ERRORS ? { code: instrument(code), map: null } : null), }] : [], test: { // quirks-reset: no remembered per-remote quirks leak between tests (#136) setupFiles: [ 'test/quirks-reset.js', ...(failsites ? ['test/space/failsite-setup.js'] : []), ...(coverage ? ['test/coverage-flush.js'] : []), ], include: ['test/**/*.test.js'], exclude: ['**/node_modules/**'], // node:test had no default timeout and the space/oracle tests run for minutes; a test that needs a bound states it // in its own options ({ timeout }), exactly as before. 0 disables vitest's 5 s / 10 s defaults. testTimeout: 0, hookTimeout: 0, // One file per child process, files in parallel (node --test's model). The tests spawn git, http servers and // worker_threads that load src/*.ts under node's type stripping; `forks` measured faster than `threads` here // (scripts/to-vitest.mjs header, PR body) and keeps process-global state (process.on('exit'), env) per file. pool: 'forks', fileParallelism: true, // Tests run in declaration order within a file, as under node:test. sequence: { concurrent: false, sequencer: WeightedSequencer }, experimental: { viteModuleRunner: !coverage }, }, });