#44·skills

(vitest): document typed `vi.mock(import(...))` module mocking guidance

Author: sebcomeauCreated Sep 30, 2026Updated Sep 30, 2026

Summary

Please add the typed module-mocking guidance from the Vitest documentation to the vitest skill, especially the recommendation to pass a dynamic import() expression to vi.mock instead of a plain module path string.

Relevant Vitest documentation:

Proposed guidance

Add a concise note and example to the module-mocking section, likely in skills/vitest/references/features-mocking.md:

typescript
// Prefer import() so TypeScript can infer and check the mocked module's types
vi.mock(import('./db.js'), () => ({
  getUser: vi.fn(),
}))

Explain that using import('./db.js') allows TypeScript to infer the module type and type-check the mock factory's return value, whereas a plain string such as vi.mock('./db.js', ...) does not provide the same type inference.

Other related documentation recommendations

While reviewing the current Vitest guide, the following topics also look useful to keep represented or cross-checked in the skill:

  • Hoisting behavior of vi.mock, vi.unmock, and vi.hoisted; use vi.doMock/vi.doUnmock for non-hoisted, runtime mocking.
  • Partial mocks with importOriginal.
  • vi.mock('./module', { spy: true }) when the real implementation should run while calls are tracked.
  • Explicit-resource cleanup with using for scoped spies and mocks.
  • Mock call arguments are stored by reference; clone values at call time when assertions must capture pre-mutation state.
  • Clear/reset/restore semantics and the relevant automatic mock configuration options.

The current skill already covers many of these areas in features-mocking.md, so this issue could either add the missing typed-import recommendation and verify the others against the upstream guide, or track any necessary follow-up improvements separately.

Existing related work

There is already an open issue requesting expect.unreachable: antfu/skills#14. This proposal is separate and focuses on module-mocking type inference and related mocking guidance.

Acceptance criteria

  • The Vitest skill documents the import() form of vi.mock for typed mock factories.
  • The example demonstrates a type-safe mocked module factory.
  • The guidance links to the relevant upstream Vitest documentation.
  • Existing mocking guidance is checked for consistency with the upstream documentation, without duplicating content unnecessarily.