Files
codegraph/vitest.config.mts
T
danusha2345andClaude Opus 5.5 b1e6c9767d test: move the test workspace to Vitest 4 and align the Vite toolchain
The repo ran two Vite stacks. ui/ builds the viewer with Vite 7 and
vite-plugin-svelte 6, while Vitest 2 at the root kept Vite 5.4 and plugin 4 with
their own esbuild, whose dev tree carried the critical audit findings. The move
leaves one stack.

Tests now run as test.projects in vitest.config.mts, so vitest.workspace.mts goes
away: engine extends the base (env, global setup, home sandbox), ui stands alone
with the Svelte plugin, jsdom and the browser conditions. The WASM flags move to
test.execArgv, because Vitest 4 ignores poolOptions.forks.execArgv with a
deprecation line and the #1779 worker death comes back. git-index-currency injects
its failures through the mocks' own vi.fn: under Vitest 4 a spy on a mocked module
is that vi.fn, so the captured real function recursed into itself, and
restoreAllMocks no longer resets one, so the injection leaked into later tests.
devEngines sets Node 22.12 as the floor for working in the repo, which is what
Vite 7 asks for; engines, what users run, is unchanged.

Changes on top of the contributor's commits:
- The lockfile is regenerated from main's, not taken from the PR: the PR's would
  have put devalue, postcss and ignore back below the fixes in issues 3044 and
  3050, and restored the @types/better-sqlite3 that 3044 dropped. ui's packages
  hoist to the root at the versions it already built with, and rollup stays.
- testTimeout and hookTimeout rise to 30 s and 60 s. Since Vitest 3 a synchronous
  test or hook that outlasts its limit fails; under Vitest 2 the timer could not
  fire until the body returned, so a body blocked in spawnSync never timed out. A
  6 s synchronous test passes on 2.1.9 and fails on 4.1.11, and an 11 s hook does
  the same and skips its tests. The first full run lost frontload-hook to this,
  the contributor's lost two bundle-launcher listings, and 31 tests passed 4 s on
  a quiet machine, so on a loaded one they would all flake.
- A new test reads the WASM flags from the worker's own command line, since the
  config can stop reaching workers with no failure, and a Vitest 5 move could
  repeat that.
- CONTRIBUTING and AGENTS state the new floor and the projects setup; two test
  files name TestProject, which replaced the removed GlobalSetupContext type.
- Left out as out of scope: a trailing blank line in ui/src/lib/trail-codec.ts.

Validated on Windows, Node 22.22, npm 10.9:
- what ships is unchanged: npm ci --omit=dev, the bundle's step, gives 193 files
  byte-identical to main's; npm run build:lib gives 171 byte-identical files;
  dist/viewer matches main's once Svelte's scope hashes, which come from the
  checkout path, are masked (main built in two folders differs the same way).
- every test is kept: at cd04b21a vitest list finds 9,719 tests on main (9,701
  engine, 18 ui) and those plus the new guard here, each in the same project, none
  twice.
- npm's own checks across Node versions: from 22.12 up, devEngines passes and no
  package warns EBADENGINE, the same as main; below it npm ci and npm run stop with
  EBADDEVENGINES. Node 20 could not run from source already, and Vite 7 rejects
  22.5 to 22.11. npm ci logs the one deprecation main logs. release.yml's lock sync
  changes only the version fields.
- dev tree audit: main 10 findings with 2 critical, here 7 with none critical;
  production stays at 0.
- red to green: main's git-index-currency fails 7 of 13 under Vitest 4; putting
  the flags back under poolOptions turns the guard red.

Original PR #1667 by @danusha2345.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 17:31:15 -05:00

104 lines
4.6 KiB
TypeScript

import { svelte } from '@sveltejs/vite-plugin-svelte';
import { defineConfig } from 'vitest/config';
import { WASM_RUNTIME_FLAGS } from './src/extraction/wasm-runtime-flags';
/**
* One process, two Vitest projects. The engine stays in Node; the UI package
* test alone gets Svelte compilation, jsdom and browser resolution conditions.
*/
export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['__tests__/**/*.test.ts'],
// Suites that spawn the built CLI need a current dist/ (#1879), and the end
// of the run removes any home sandbox its own test file could not.
globalSetup: ['./__tests__/global-setup-dist.ts', './__tests__/global-setup-home-sandbox.ts'],
// A throwaway home dir (and git global config) per test file, so nothing
// the suite runs can write to the developer's real one (#2275).
setupFiles: ['./__tests__/setup-home-sandbox.ts'],
/**
* Since Vitest 3 a synchronous test or hook fails when it outlasts its
* timeout. Under Vitest 2 the timer could not fire until the body had
* returned, so a body blocked in `spawnSync` never timed out at all. Hundreds
* of tests here block on the built CLI, and on a loaded machine the 5 s and
* 10 s defaults would turn them into flakes.
*/
testTimeout: 30_000,
hookTimeout: 60_000,
/**
* Several MCP integration tests (mcp-daemon, mcp-initialize, mcp-ppid-watchdog,
* mcp-roots) spawn `dist/bin/codegraph.js serve --mcp` with `process.execPath`
* and rely on the child inheriting `process.env`. On a Node >= 25 dev machine
* the CLI's hard-block (src/bin/codegraph.ts) would otherwise exit the child
* before it ever responds, so every spawn-based test times out — see #478.
*
* Setting the override here keeps the CLI's runtime guard intact for end
* users (it's still enforced when `codegraph` is invoked directly) while
* letting the test suite run on whatever Node the contributor happens to
* have installed. CI on Node 22/23 is unaffected — the guard doesn't fire
* there, so the variable is a no-op.
*/
env: {
CODEGRAPH_ALLOW_UNSAFE_NODE: '1',
/**
* The suite spawns real CLI/MCP processes; without this they would write
* telemetry state into the contributor's real ~/.codegraph and count test
* tool calls as real usage. The telemetry unit tests are unaffected —
* they inject their own `env` via the Telemetry constructor.
*/
CODEGRAPH_TELEMETRY: '0',
},
/**
* The same V8 flags every real launch path passes (the bundled launcher,
* the CLI's self re-exec, refresh-launcher): keep tree-sitter grammar
* compilation on the Liftoff baseline tier. Without them a pool worker
* runs the grammars on the turboshaft optimizing tier, and once enough
* parses have warmed a grammar function up, its background tier-up job
* exhausts a compiler Zone and aborts the worker — `Fatal process out of
* memory: Zone`, surfaced by vitest only as "Worker exited unexpectedly"
* with the rest of the file's tests silently unrun (#1779, #1883; the
* product-side story is in wasm-runtime-flags.ts, #293/#298). V8 flags are
* process-global, so the parse worker threads a test spawns are covered
* too. Vitest 4 reads `test.execArgv`; the 2.x `poolOptions.forks.execArgv`
* is silently ignored here. The engine project inherits it via `extends`.
*/
execArgv: [...WASM_RUNTIME_FLAGS],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],
},
projects: [
{
extends: true,
test: {
name: 'engine',
include: ['__tests__/**/*.test.ts'],
exclude: ['**/node_modules/**', '**/dist/**', '__tests__/ui-package.test.ts'],
},
},
{
// Browser package resolution must not leak into the engine project:
// web-tree-sitter and other dual packages would resolve differently.
// And no `extends`: a project that extends takes the base's `include`
// too, so every engine suite would run a second time inside jsdom.
extends: false,
plugins: [svelte({ configFile: 'ui/svelte.config.js' })],
resolve: { conditions: ['browser'] },
test: {
name: 'ui',
globals: true,
include: ['__tests__/ui-package.test.ts'],
environment: 'jsdom',
server: {
deps: {
// @xyflow/svelte ships source .svelte files that Node cannot load.
inline: [/@xyflow\/svelte/],
},
},
},
},
],
},
});