Mocking
Mock boundaries and own their cleanup; prefer dependency injection when it keeps the production interface simpler.
vi.mockis hoisted. Keep it at file scope (v5 throws otherwise); put handles used by its factory invi.hoisted. Its specifier must be identical to the productionimportspecifier, and it cannot mockrequire().- Create a fresh result from every stateful mock factory per test. Keep one-off module wiring in that file; share only repository-conventional factories.
- Install fake timers before code schedules work. Use async advancement when callbacks schedule promises, and always
restore real timers. Do not combine fake timers with uncontrolled real waits. From v5, fake timers and
vi.setSystemTimealso mock a globalTemporal. - From v5,
clearMocksdefaults totrue: call history is cleared before each test, so assert on calls made inside that test unless the repository setsclearMocks: false. - Restore owned spies, timers, environment stubs, and replaced global property descriptors in local cleanup.
vi.restoreAllMocks()restoresvi.spyOnimplementations but neither clears their history nor restores automocks; reset or reconfigure automocked exports explicitly.
See the installed version's Vi API for factory, timer, and stub semantics.