msw

Configure MSW v2 request handlers and mock HTTP and GraphQL APIs in tests.

Updated Sep 5, 2026
One-click install
npx skills add https://github.com/pohlai88/afenda-xforge-v5 --skill msw-pohlai88
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: msw
Source: https://github.com/pohlai88/afenda-xforge-v5/tree/main/.agents/skills/msw
Command: npx skills add https://github.com/pohlai88/afenda-xforge-v5 --skill msw-pohlai88

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires msw, and includes references (resource) and assets (resource) components.

What problem does it solve? Setting up MSW (Mock Service Worker) v2 incorrectly leads to silent handler failures, flaky tests, and mock drift between development and test environments. This Skill provides 45 prioritized rules across 8 categories so AI agents and developers configure, write, and debug API mocks correctly. ## Core Features & Use Cases - Setup & Handler Architecture Rules: Covers critical configuration like Node.js entrypoints, lifecycle hooks, absolute URLs, v2 response syntax, and explicit request body parsing. - Test Integration & Response Patterns: Guides handler resets, concurrent test boundaries, realistic delays, error simulation, streaming responses, and custom headers. - GraphQL & Advanced Mocking: Explains operation-name matching, variables access, batched queries, cookies/auth, file uploads, and passthrough requests. - Use Case: When writing a test that mocks a REST endpoint, apply the handler rules to destructure resolver arguments correctly, parse the request body explicitly, and return an HttpResponse with the proper status and headers. ## Quick Start Ask the agent to review your MSW handler file and apply the MSW best practices rules to fix any incorrect v2 patterns.

Frequently Asked Questions about msw

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

FAQPage Schema
How do I set up MSW v2 for Node.js tests?

Use setupServer from the msw/node entrypoint with your shared handlers, then configure lifecycle hooks in test setup: server.listen() in beforeAll, server.resetHandlers() in afterEach, and server.close() in afterAll. Node.js 18 or higher is required.

How do I mock GraphQL queries with MSW?

Use graphql.query or graphql.mutation handlers matched by operation name rather than URL, since all GraphQL requests share one endpoint. Access variables via the resolver's variables property and return errors in the standard errors array format with status 200.

Why is my MSW handler not matching requests?

Common causes include relative URLs in Node.js environments, wrong handler ordering where general handlers shadow specific ones, and unset environment variables in handler URLs. Use absolute URLs or wildcards like */api/user and order handlers from specific to general.

Does MSW v2 automatically parse request bodies?

No, MSW v2 does not auto-parse request bodies based on Content-Type. You must explicitly call request.json(), request.text(), request.formData(), or request.arrayBuffer() inside the resolver to access body content.

How do I test error states with MSW handlers?

Define happy-path handlers as your baseline, then use server.use() inside individual tests to override specific endpoints with error responses such as status 401, 500, or HttpResponse.error() for network failures. Reset handlers after each test to avoid pollution.

When should I not use MSW for API mocking?

MSW is not needed for unit tests of pure business logic without network calls, and fully mocked environments should avoid bypass() passthrough to real APIs. Projects still on MSW v1 must use the older res(ctx) syntax until migrating.