Automocking, spy: true & __mocks__ in Vitest
From the Vitest cheat sheet · Module Mocking · verified Nov 2026
Automocking, spy: true & __mocks__
Automock a module, spy on all its exports, use manual mocks, and mock dynamically
typescript
vi.mock('./math') // automock: functions return undefined
vi.mock('./math', { spy: true }) // keep real code, track calls
vi.mock('axios') // uses __mocks__/axios.ts if present📌 __mocks__ files load only when vi.mock() is called, unlike Jest
💡 { spy: true } is the Browser Mode way to spy on module exports
⚡ vi.doMock isn't hoisted, so it can use variables from the test
⚠️ Modules imported in setupFiles are cached before vi.mock runs
automock__mocks__vi.doMockspy
Continue with Vitest
Save the full cheat sheet or work through every related task.