Skip to content

test(studio): run vitest without isolation in web and server - #2203

Merged
kantord merged 2 commits into
mainfrom
studio-vitest-settings
Oct 8, 2026
Merged

kantord merged 2 commits into
mainfrom
studio-vitest-settings

Conversation

@kantord

@kantord kantord commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Summary

Studio (apps/web, apps/server) now runs vitest with isolate: false. Tests share one module cache per worker, which removes per-file environment setup and, more importantly, makes cross-file state leaks fail loudly while the suite is still small instead of rotting in.

  • Guard: two Biome GritQL plugins (apps/lint/) fail the build on vi.mock/vi.doMock and on direct Web Storage spies. Their messages point at the NO-ISOLATE RULES comment at the top of apps/web/src/test-setup.ts, which lists each known-bad pattern next to its idiomatic fix.
  • Shared reset: test-setup.ts runs after every test: RTL cleanup, real timers, text selection, Web Storage, account-storage and api-client module state, and the shared generated client's baseUrl/fetch. Spies, stubbed globals and env use vitest's restoreMocks/unstubGlobals/unstubEnvs flags.
  • Migration: all 9 vi.mock files (8 in web, 1 in server) plus 3 that landed on main meanwhile now render real modules under real providers and fake the network at the apiClient/fetch boundary.

Findings worth knowing

  • vi.restoreAllMocks() and the restoreMocks flag do not restore a spy on happy-dom's localStorage; one leaked spy broke ~30 unrelated tests. spyOnLocalStorage() (web/src/test-storage.ts) registers its own restore, and Biome bans direct storage spies.
  • The happy-dom window/document are shared across files under isolate: false (properties, <body> attributes, the text selection).
  • Once any file has built a TanStack router, a route module first evaluated afterwards shares options with that router's root. Root cause not established; test-setup.ts evaluates the production route tree first, which avoids it (commented there).

Review notes

  • Non-test source changed in two places: createApp takes an optional studioBuildId (replaces a vi.mock of build-info.js); api-client.ts gains resetApiClientState().
  • Weakened assertions: the production-route test in error-routes.test.tsx can no longer put the private sentinel into a real render crash, so it asserts the sentinel and the raw Cannot read properties text are absent. The four test-router tests still throw the sentinel directly. Two global-search assertions moved from SDK-call arguments to what is visible on the wire (cache: no-store, no query string).
  • Known flake: global-search.test.tsx > checks fresh identity before reopening cached private results fails in roughly a quarter to a half of shuffled serial runs (--no-file-parallelism --sequence.shuffle.files), never in normal runs, and passes 20/20 alone and with isolation on. Minimal polluter: chat-workspace.transcript.integration.test.tsx. Cause not found.
  • Not verified: apps/server's integration suite (needs the mecated binary) and sdk/typescript, which keeps default isolation and is untouched.

Not ported: sdk/typescript

sdk/typescript and sdk/typescript/examples/slack-bot keep vitest's default isolation. They were not touched and were not assessed (different package, probably a node environment rather than happy-dom). The method below is what worked here, so a future port can follow it.

Method for porting a package to isolate: false
  1. Baseline, then flip with the CLI flag. Run the normal suite, then vitest run --no-isolate with no config change. Failures that differ between runs are scheduling-dependent leaks.
  2. Make it deterministic before debugging. --no-file-parallelism (one worker) plus a fixed file order. A tiny BaseSequencer subclass that sorts by an explicit order list (env var) lets you run any [polluter, victim] subset in a chosen order. Alphabetical order hides leaks whose victim sorts before the polluter, so also sweep --sequence.shuffle.files --sequence.seed=N over 10+ seeds.
  3. Find polluters by delta debugging, not reading. For each failing file: confirm it passes alone, run every [P, victim] pair (parallelised, ~3s each) to list single-file polluters, and ddmin over all predecessors when no single file reproduces it. For an intermittent failure, capture the file order of a failing run (JSON reporter, sort by startTime) and count a subset as failing if any of ~4 trials fails.
  4. Check the failure with isolation forced on (--isolate). If it still fails, it is the test, not the isolation setting.
  5. Fix in this order:
    • one shared afterEach in a setup file for global state (storage, selection, timers, module-level singletons with a reset function owned by the module);
    • vitest's restoreMocks / unstubGlobals / unstubEnvs flags for spies, stubs and env;
    • remove vi.mock (real modules, real providers, fake the network at the client boundary) and enforce that with a lint rule so it cannot come back;
    • explicit cleanup() for Testing Library, whose auto-cleanup only registers in the first file of a worker.
  6. Probe tool/library assumptions with a two-file repro (file A mutates, file B observes) before trusting a theory. Several plausible-looking causes here were wrong until probed (Storage prototype staleness, restoreAllMocks on localStorage).
  7. Do not add blanket resets pre-emptively (document.body.innerHTML = "") or call happyDOM.abort() after each test: the latter leaves the window unusable (385 failures here). Add a reset only for state a shuffled serial run proves is shared.

The throwaway tooling (an order sequencer config, a pair scanner and a ddmin script, ~100 lines in total) was not committed.

Test plan

  • cd apps && pnpm check (Biome, typecheck, server 182/182, web 651/651, generated files in sync)
  • Delta-debugged shuffled serial runs: every pollution found was fixed except the flake above
  • CI

Fully or partially written by an AI agent.

🤖 Generated with Claude Code

@kantord
kantord force-pushed the studio-vitest-settings branch from bf30d39 to 66cba0c Compare October 8, 2026 16:14
@kantord
kantord marked this pull request as ready for review October 8, 2026 16:15
@kantord
kantord merged commit e70ad22 into main Oct 8, 2026
31 of 55 checks passed
@kantord
kantord deleted the studio-vitest-settings branch October 8, 2026 16:28
@toolhive-release-app toolhive-release-app Bot mentioned this pull request Oct 13, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants