mocking-strategies

Implement Vitest mocking strategies for JavaScript and TypeScript unit tests.

2|1|Updated Nov 13, 2025
One-click install
npx skills add https://github.com/MolcajeteAI/plugin --skill mocking-strategies
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mocking-strategies
Source: https://github.com/MolcajeteAI/plugin/tree/main/deprecated/tech-stacks/js/common/skills/mocking-strategies
Command: npx skills add https://github.com/MolcajeteAI/plugin --skill mocking-strategies

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides comprehensive guidance and examples for effectively mocking dependencies in Vitest, ensuring isolated and reliable unit tests.

Core Features & Use Cases

  • Isolate Units: Mock external services, APIs, or modules to focus testing on the unit under test.
  • Create Test Doubles: Utilize vi.fn(), vi.spyOn(), and vi.mock() for stubs, spies, and fakes.
  • Use Case: When testing a function that calls an external API, use this skill's techniques to mock the API response, allowing you to test your function's logic without making actual network requests.

Quick Start

Use the mocking-strategies skill to learn how to mock a function's return value using vi.fn().

Frequently Asked Questions about mocking-strategies

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

FAQPage Schema
How do I mock an external API call in Vitest unit tests?

To mock external API calls in Vitest unit tests, you can use `vi.fn()` to create stubs or `vi.mock()` to replace the module making the network request, allowing you to test function logic without actual network requests.

What is the best way to mock Node.js modules and npm packages in Vitest?

The best way to mock Node.js modules and npm packages in Vitest is using `vi.mock()` to replace module implementations with test doubles, ensuring your unit tests remain isolated from external dependencies.

When should I use vi.spyOn versus vi.fn for mocking methods in JavaScript testing?

Use `vi.spyOn()` to track method calls on existing objects without changing behavior, and `vi.fn()` to create standalone mock functions or stubs. Both provide mock assertions for effective JavaScript unit testing.

Can I mock timers in Vitest for JavaScript and TypeScript projects?

Yes, you can mock timers in Vitest for JavaScript and TypeScript projects. Vitest provides timer mocking strategies to control time-dependent logic, ensuring reliable and isolated unit tests for asynchronous operations.

How do I assert mock calls and return values during Vitest unit testing?

You assert mock calls and return values in Vitest unit testing by utilizing the assertion properties attached to `vi.fn()` and `vi.spyOn()` test doubles. This verifies that your units interact with dependencies exactly as expected.