vitest-mocking

Automate Vitest mocking patterns for modules, globals, and imports.

Updated Feb 20, 2026
One-click install
npx skills add https://github.com/emiltsokov/weeklyvaibe --skill vitest-mocking-emiltsokov
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: vitest-mocking
Source: https://github.com/emiltsokov/weeklyvaibe/tree/main/.agents/skills/vitest-mocking
Command: npx skills add https://github.com/emiltsokov/weeklyvaibe --skill vitest-mocking-emiltsokov

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Vitest mocking patterns and test double strategies. Use when writing tests that need mocks, spies, stubs, or fakes. Triggers include: mocking modules, stubbing globals (fetch, Date, timers), testing composables, mocking imports (default/named), creating test doubles, replacing dependencies, verifying mock calls, cleaning up mocks, or any question about "how do I mock X in Vitest". Also use when reviewing tests for proper mocking practices.

Core Features & Use Cases

  • Mock modules and replace dependencies to isolate units
  • Stub globals (e.g., fetch, Date) and create test doubles
  • Verify interactions and cleanup mocks across test suites
  • Use cases include testing components, utilities, composables, and API clients with controlled behavior

Quick Start

Create a simple test that mocks a module and asserts a function is called with expected arguments.

Frequently Asked Questions about vitest-mocking

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I mock a module in Vitest?

To mock a module in Vitest, you use the vi.mock function to replace the module's exports with test doubles. This isolates the unit under test by intercepting named or default imports and substituting controlled behavior.

What is the best way to stub globals like fetch and Date in Vitest?

The best way to stub globals like fetch and Date in Vitest is using vi.stubGlobal to replace them with test doubles. This allows you to control external interactions and time-based logic for reliable, repeatable tests.

Why does my Vitest mock hoisting cause errors?

Vitest mock hoisting causes errors because vi.mock is lifted to the top of the file before imports execute, breaking variable references. You must use inline mocks or factory functions to avoid scope and hoisting issues.

How do I verify mock calls in Vitest?

You verify mock calls in Vitest by using assertion methods like toHaveBeenCalledWith on your mocked functions or spies. This checks that your unit under test interacted with dependencies exactly as expected.

Can I create test doubles for composables and utilities in Vitest?

Yes, you can create test doubles for composables and utilities in Vitest by mocking their module imports or stubbing global dependencies. This ensures components and hooks are tested in isolation with controlled behavior.

When should I clean up mocks across test suites in Vitest?

You should clean up mocks across test suites in Vitest after each test to prevent state leakage. Restoring mocks and stubs ensures every test runs in isolation and produces reliable, repeatable results.