🟢 PR #6056 — ozone: migrate tests to vitest and load the dev-env graph once per worker
Pull request (OPEN) · PR #6056 ozone: migrate tests to vitest and load the dev-env graph once per worker · @matthieusieben Change
Ozone's 107 integration test files ran under jest. Jest gives every test file its own module registry, so each file re-imported the whole dev-env graph (PDS, AppView, ozone, bsync, PLC), and jest-config has no switch to share it across files. This PR moves ozone to vitest and applies the same import-overhead fix #6052 applied to bsky:
packages/ozone/vitest.config.ts follows bsky's: workspace dist loads natively ( server.deps.external: [/\/packages\/.+\/dist\//] ), and the suite runs with isolate: false , so the dev-env graph is imported once per worker. There are no src unit tests, so a single project is enough. Test timeout stays at 60s. Hook timeout is 60s, matching jest, whose testTimeout also covered hooks.
test is now ../dev-infra/with-test-redis-and-db.sh vitest run , and test:updateSnapshot is ... vitest run --update . jest.config.cjs , the NODE_OPTIONS=--experimental-vm-modules prefix and ozone's jest / @jest/globals devDependencies are removed. The root jest tooling stays, since pds , bsync and sync still use it.
tsconfig.test.json now extends tsconfig/vitest.tsconfig.json , and tsconfig.config.json covers vitest.config.ts . The stale jest.setup.ts entries are gone; that file no longer exists.
API swaps only. jest.* becomes vi.* , and each file gets an explicit vitest import. Where vitest differs:
jest.replaceProperty(obj, key, v) becomes vi.spyOn(obj, key, 'get').mockReturnValue(v) , restored by the existing restoreAllMocks / finally .
content-tagger's spread-env variant becomes vi.stubEnv + vi.unstubAllEnvs() .
Fake timers' doNotFake: [...] becomes toNotFake: [...] with the same list, so only Date is faked, as before.
No test or assertion was dropped or rewritten. The only change on an expect line is in moderation-appeals , which had an unawaited expect(sc.createReport(...)).rejects.toThrow('You cannot appeal this report') . Jest never checked it. Vitest 5 fails unawaited async assertions, so this PR adds await . The assertion now runs, and passes.
CI wiring is untouched ( #6045 handles it). Ozone stays in the integration set, and the existing --maxWorkers=1 --shard=k/8 --passWithNoTests flags pass straight through to vitest run .
Parity
run
files
tests
passed
skipped
failed
snapshots
jest baseline ( main @ 32e4abf ), 8 shards, --maxWorkers=1 --shard=k/8
107
1399
1398
1
0
44/44 matched
vitest, 8 shards, same flags
107
1399
1398
1
0
44/44 matched
vitest, whole suite, one worker, shuffled (seed 1234)
107
1399
1398
1
0
44/44 matched
vitest, whole suite, one worker, shuffled (seed 98765)
107
1399
1398
1
0
44/44 matched
The skipped test is the same in both runners: the pre-existing it.skip in moderation.test.ts . Per-file test names match, except for 32 it.each titles that vitest formats differently: arrays print as [ '*' ] instead of ["*"] , and long %s values are cut at 40 characters with … . None of those tests has a snapshot.
Snapshots
Vitest keys snapshots describe > test N , while jest keys them describe test N , and the file header differs.
First, the suite ran against the untouched jest .snap files with -u none . Every failure was a key-format problem:
34 tests failed with Snapshot `<describe> > <test> 1` mismatched , for keys missing from the file.
All 44 jest keys across the 21 files were reported obsolete.
Next, the files were converted mechanically. The header and all 44 keys were rewritten using ancestorTitles / title from the jest JSON results. The diff touches only those 65 lines (21 headers and 44 exports[...] keys). No value changed.
Then the suite ran again with vitest run -u none : 44/44 matched , and nothing was unmatched, obsolete or written. -u was never needed.
Finally, both versions of each file were parsed, with keys normalized ( ' > ' → ' ' ). All 44 values are byte-identical to the jest originals.
Isolation audit ( isolate: false )
Module state now outlives a file within a worker. These are the places where tests patch shared state, and how each is restored:
process.env
online-members-config and report-tags-config replace the whole env with a getter spy. The existing afterEach(vi.restoreAllMocks) restores it.
blob-diverter uses a getter spy, restored by the existing finally { vi.restoreAllMocks() } .
content-tagger uses vi.stubEnv and calls vi.unstubAllEnvs() right after readEnv() .
dev-env never reads OZONE_* vars.
Global fetch
video-invalidator restores in afterEach .
pds-client restores in try/finally .
blob-diverter uses using .
safe-fetch used to restore only on the success path. It now uses using , so a failing test can't leak a mocked fetch into later files.
undici global dispatcher ( blob-diverter ): reset in the existing finally .
Fake timers ( report-stats , report-closure-target-stats ): fake Date only, in try/finally { vi.useRealTimers() } .
Prototype, static and Date.now spies ( pg.Client.prototype.query , service prototypes, OzoneService.create , Date.now ): all use using .
prom-client : report-reason passes its own Registry and asserts that the default registry stays empty. Ozone only registers metrics when it's given a registry, so the shared default registry stays empty across files.
Nothing calls vi.mock , assigns process.env.X , or adds process listeners. Module-level state in the src modules that tests import is constant (Sets/Maps of literals).
Shuffled runs, whole suite, one worker: --maxWorkers=1 --sequence.shuffle.files --sequence.seed=<n> --logHeapUsage . Both seeds were green; numbers are in the parity table. Heap was logged after each file:
seed
min
median
p90
max
per-quarter floor
1234
175 MB
229 MB
286 MB
617 MB
175 → 198 → 201 → 214 MB
98765
189 MB
279 MB
468 MB
753 MB
189 → 225 → 193 → 222 MB
There is no growth trend. The peaks are transient and come from the heavier report files. bsky stayed at 250–435 MB under the same check.
Timing
Two CI-like shards on each runner, all single worker, interleaved over 4 rounds. Round 1 ran jest first, round 2 ran vitest first, and rounds 3–4 alternated jest and vitest run by run. Jest got a fresh --cacheDirectory each time, like CI's cold cache. Jest and vitest hash paths differently, so --shard=k/8 selects different files in each runner. The "vitest, jest's files" column runs vitest on exactly the files jest's shard k ran.
shard
jest --shard
vitest --shard
vitest, jest's files
3/8
107, 188, 168, 213 (median 178s)
57, 94, 82, 92 (median 87s)
106, 108, 113, 132 (median 110.5s)
6/8
69, 135, 127, 125 (median 126s)
84, 122, 185, 110 (median 116s)
86, 77, 107, 97 (median 91.5s)
Vitest's own breakdown across these runs:
tests: 87–98%
import: 2–8% (≈2–9s, paid once per worker)
transform: ≤5% (≈1–5s)
Caveat: this was a busy shared desktop. Load average ran from ~4 in round 1 to ~19 in round 4, and Postgres runs in a Colima VM shared with other sessions. The same file's duration varied up to 2× between runs.
Under contention (rounds 2–4), vitest on jest's own files took 0.57–0.84× jest's time.
In the quiet round 1, it tied on shard 3 (106 vs 107s) and lost on shard 6 (86 vs 69s). Most of that loss is one outlier: report-reason took 17.9s, against 9–11s in its five other vitest runs.
To isolate the per-file overhead from database noise, a probe ran the same 6 small files (one network + seed each) back to back at load ~5–7. Jest took 31s and 33s; vitest took 22.4s and 23.0s, with a 14% import share paid once. That is about 1.3s saved per file, roughly the cost of importing the dev-env graph (about 1.9s natively, warm).
For reference, all 8 shards with 4 running in parallel (not interleaved) summed to 1142s on jest and 707s on vitest. The CI run on this PR is the real number to compare against the pre- #6053 baseline (run 46003673009: ≈1813s across 8 shards).
Commands, run from packages/ozone against a fresh database per run:
REDIS_HOST=127.0.0.1:6380 LOG_ENABLED=false
# jest baseline (main @ 32e4abfe0)
NODE_OPTIONS=--experimental-vm-modules pnpm exec jest --maxWorkers=1 --shard=3/8 --passWithNoTests --cacheDirectory=
# vitest, CI-like
pnpm exec vitest run --maxWorkers=1 --shard=3/8 --passWithNoTests
# vitest on jest's shard-3 files
pnpm exec vitest run --maxWorkers=1 tests/3p-labeler.test.ts tests/ack-all-subjects-of-account.test.ts tests/appeal-actioned-subject.test.ts tests/expiring-tags.test.ts tests/get-starter-pack.test.ts tests/inbox-notification-start.test.ts tests/moderation-appeals.test.ts tests/moderation-events.test.ts tests/queue-tag-routing.test.ts tests/report-closure-target-stats.test.ts tests/scheduled-action-processor.test.ts tests/server.test.ts tests/strike-expiry-processor.test.ts tests/verification.test.ts
# isolation check
pnpm exec vitest run --maxWorkers=1 --sequence.shuffle.files --sequence.seed=1234 --logHeapUsage" data-sanitized-class="highlight highlight-source-shell notranslate position-relative overflow-auto"> export DB_POSTGRES_URL=postgresql://pg:password@127.0.0.1:5433/ < fresh_db > REDIS_HOST=127.0.0.1:6380 LOG_ENABLED=false
# jest baseline (main @ 32e4abfe0)
NODE_OPTIONS=--experimental-vm-modules pnpm exec jest --maxWorkers=1 --shard=3/8 --passWithNoTests --cacheDirectory= < fresh_dir >
# vitest, CI-like
pnpm exec vitest run --maxWorkers=1 --shard=3/8 --passWithNoTests
# vitest on jest's shard-3 files
pnpm exec vitest run --maxWorkers=1 tests/3p-labeler.test.ts tests/ack-all-subjects-of-account.test.ts tests/appeal-actioned-subject.test.ts tests/expiring-tags.test.ts tests/get-starter-pack.test.ts tests/inbox-notification-start.test.ts tests/moderation-appeals.test.ts tests/moderation-events.test.ts tests/queue-tag-routing.test.ts tests/report-closure-target-stats.test.ts tests/scheduled-action-processor.test.ts tests/server.test.ts tests/strike-expiry-processor.test.ts tests/verification.test.ts
# isolation check
pnpm exec vitest run --maxWorkers=1 --sequence.shuffle.files --sequence.seed=1234 --logHeapUsage
Agent files
.agents/skills/testing/SKILL.md : ozone moves from the jest list to the vitest list in "Choosing a runner". This overlaps with #6046 , which rewrites the same paragraph. The edit here only moves the one name, so whichever lands second has a trivial conflict.
references/jest.md : ozone is dropped from the jest packages that reference dev-env.
references/vitest.md : ozone is added to the packages that customize their vitest config.
#6046 also touches the jest.md and vitest.md lines.
🤖 Generated with Claude Code Ouvrir sur GitHub →