Use this reference for Vitest and similar in-process unit-test runners.
Read the active Vitest configuration, its full setupFiles chain, package-level
overrides, and the repository's unit-test guidance. In this repository, inspect:
vite.config.mtsvitest.setup.tsandvitest.timer.setup.ts- package-level Vitest configuration and setup
docs/guidance/vitest.mddocs/testing/vitest-patterns.md
Check behavior against the installed Vitest version. Defaults change, and setup files compose.
Use native configuration for lifecycle behavior Vitest already supports. Use a setup file only for invariants configuration cannot express. Keep package-only behavior out of repository-wide setup.
Separate runner-managed resets, fresh mutable state, process-wide modes that need restoration, and scenario state that must remain local. Add hooks only for the part of the contract Vitest does not already own.
A global default must fit every test in scope. Find the common behavior from the tests, then preserve deliberate opt-outs. When choosing a deterministic baseline, keep absolute values local when the value itself is under test.
Runner-level reset and restoration can change module-scope test doubles. Check whether their default implementations survive and whether tests reprogram them during a scenario.
Share a module mock only when it reduces reader effort and preserves behavior. Keep assertion handles explicit. Test module re-evaluation because it can replace shared mock state while a test still holds the old identity.
Keep test-specific vi.mock calls in the test module. A call inside an imported
helper is not hoisted ahead of that test module's static imports. For shared
implementations, use __mocks__ with a local vi.mock. For runtime selection,
call vi.doMock before a subsequent dynamic import.
Check whether a setup file imports the mock target, directly or transitively.
Vitest cannot replace that cached module. Prefer removing the setup import. If it
is unavoidable, call vi.resetModules() inside vi.hoisted before the test
imports the target, and account for setup and test code holding different module
instances.
Skip the extraction if typed shared code is no simpler than the local mocks or needs global mutable indirection.
Work on one lifecycle responsibility at a time:
- Count and classify local occurrences.
- Remove them without adding a replacement.
- Use failures to find the contract and exceptions.
- Prefer configuration, then the narrowest setup file.
- Remove only calls the new owner makes redundant.
- Keep mid-test resets and scenario state local.
Subagents may check independent suites, but each must report observed necessity and exceptions.
Run affected files, setup-contract tests, static checks, and the complete unit suite. Shared process state can fail only when unrelated suites run together.
Profile changes that execute for every test. Compare several equivalent runs and report variance instead of treating normal noise as a result.