msw-fetch-mock

Configure MSW v2 handlers to mock HTTP APIs in Vitest.

11|2|Updated Feb 5, 2026
One-click install
npx skills add https://github.com/recca0120/code-quest --skill msw-fetch-mock
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: msw-fetch-mock
Source: https://github.com/recca0120/code-quest/tree/main/.claude/skills/msw-fetch-mock
Command: npx skills add https://github.com/recca0120/code-quest --skill msw-fetch-mock

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill solves flaky, slow, and hard-to-control frontend tests by letting you mock HTTP requests deterministically instead of relying on real network services.

Core Features & Use Cases

  • API mocking with MSW v2 in Vitest: Define request handlers using http and return typed HttpResponse objects for JSON and other payload types.
  • Per-test overrides: Use server.use(...) to customize handlers for a single test while server.resetHandlers() clears overrides automatically.
  • Network error simulation: Trigger realistic failure modes (for example HttpResponse.error()) to validate error UI and retry logic.

Quick Start

Configure your MSW handlers and server for Vitest, then run your component or integration tests while the mocked endpoints respond with the data you specified.

Frequently Asked Questions about msw-fetch-mock

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

FAQPage Schema
How do I mock fetch requests in Vitest using MSW?

Use MSW v2 `http` handlers to intercept fetch requests and return controlled `HttpResponse` objects for deterministic API mocking in Vitest. This prevents flaky and slow frontend tests caused by real network dependencies.

Can I simulate network errors and API failures with MSW in Vitest?

Yes, you can simulate network errors in Vitest by triggering realistic failure modes like `HttpResponse.error()` within your MSW handlers, allowing you to validate error UI states and retry logic deterministically.

How do I override API mock responses for a single test in Vitest?

Override API mock responses for a single Vitest test using `server.use(...)` to inject custom handlers. The `server.resetHandlers()` lifecycle call automatically clears these overrides for subsequent tests.

Does MSW v2 support parsing request bodies and path params for Vitest integration tests?

MSW v2 supports parsing request bodies via the standard Request API and handling path parameters. This allows your Vitest integration tests to return dynamic JSON payloads and cookie-based responses based on request inputs.

What lifecycle methods are required to manage an MSW server in Vitest?

Managing an MSW server in Vitest requires calling `server.listen()` to start intercepting requests, `server.resetHandlers()` to clear per-test overrides, and `server.close()` to clean up after tests finish.

Why should I use MSW for API mocking instead of mocking fetch directly in Vitest?

Using MSW for API mocking intercepts requests at the service worker level, providing controlled responses for JSON payloads and network failures without modifying your application's fetch implementation, resulting in more reliable Vitest component tests.