---
name: "msw-mock-service-worker-api-tests"
description: "Mock REST and GraphQL APIs at the network level for tests, storybook, and development with MSW."
metadata:
  author: "Artem Zakharchenko"
  version: "1.0.0"
  runtime_compatibility:
    - "codex"
    - "claude-code"
    - "cursor"
    - "windsurf"
triggers:
  - "setup msw 2.0 handlers"
  - "mock http request msw"
  - "msw in vitest"
permissions:
  fs_read: true
  fs_write: "workspace"
  shell_exec: false
  net_access: false
---

# Mock Service Worker (MSW) Network-Level API Mocking

Implements Mock Service Worker (MSW 2.0): http and graphql handlers (http.get, http.post), HttpResponse.json, setting up setupServer for Node.js tests, setupWorker for browsers, and intercepting fetch requests seamlessly.

## When to Invoke
Invoke this skill whenever you need to:
- setup msw 2.0 handlers
- mock http request msw
- msw in vitest

## Execution SOP & Rules
1. Define handlers in src/mocks/handlers.ts: http.get('/api/user', () => HttpResponse.json({ id: 1, name: 'Alex' })).
2. Configure Node server for tests: const server = setupServer(...handlers); beforeAll(() => server.listen());.
3. Override handlers per test: server.use(http.get('/api/user', () => new HttpResponse(null, { status: 404 }))).
4. Verify test code exercises actual fetch/axios code without modifying production URLs.

## Verification Checklist
- Run local linting, syntax validation, and type-checks before reporting completion.
- Ensure all created or modified files adhere to workspace boundaries and avoid uncommitted secrets.
- Verify tests or build commands exit cleanly with return code 0.
