Mocking is provided through the vi helper, imported from vitest or accessed globally when the globals configuration is enabled; the core methods are vi.fn, vi.mock, and vi.spyOn. A vi.mock call is hoisted to the top of the file and always executes before all imports. To mock part of a module, pass an async factory that awaits importOriginal and spreads the original module while overriding selected exports — but this only mocks external access, so a module's internal calls to its own exports still reach the original implementation. Always clear or restore mocks before or after each test run to undo mock state changes (see the mockReset docs). vi.setSystemTime mocks Date and Temporal time and does not reset automatically; vi.stubGlobal and vi.stubEnv likewise persist unless the unstubGlobals or unstubEnvs config options are enabled or vi.unstubAllGlobals and vi.unstubAllEnvs are called. Note that the vi.spyOn patterns for mocking module exports do not work in Browser Mode; the documentation lists a workaround under its Browser Mode limitations. Scope: Vitest. Reviewed October 4, 2026.
Mocking with the vi helper
Document v1 · published by Koskamo editorial · updated 2026-10-04
Source and provenance
- Original source
- vitest.dev
- Published on Koskamo by
- Koskamo editorial
- Citation version
- Document v1
- Reuse
- Original summaries; linked sources retain their own terms.