msw-mocking
Mock Service Worker (MSW) 2.x for API mocking. Use when testing frontend components with network mocking, simulating API errors, or creating deterministic API responses in tests.
Works with
---
name: msw-mocking
description: Mock Service Worker (MSW) 2.x for API mocking. Use when testing frontend components with network mocking, simulating API errors, or creating deterministic API responses in tests.
license: MIT
---
# MSW (Mock Service Worker) 2.x
Network-level API mocking for frontend tests using MSW 2.x.
## Quick Reference
```typescript
// Core imports
import { http, HttpResponse, graphql, ws, delay, passthrough } from 'msw';
import { setupServer } from 'msw/node';
// Basic handler
http.get('/api/users/:id', ({ params }) => {
return HttpResponse.json({ id: params.id, name: 'User' });
});
// Error response
http.get('/api/fail', () => {
return HttpResponse.json({ error: 'Not found' }, { status: 404 });
});
// Delay simulation
http.get('/api/slow', async () => {
await delay(2000);
return HttpResponse.json({ data: 'response' });
});
// Passthrough (NEW in 2.x)
http.get('/api/real', () => passthrough());
```
## Test Setup
```typescript
// vitest.setup.ts
import { beforeAll, afterEach, afterAll } from 'vitest';
import { server } from './src/mocks/server';
beforeAll(() => server.listen({ onUnhandledRequest: 'error' }));
afterEach(() => server.resetHandlers());
afterAll(() => server.close());
```
## Runtime Override
```typescript
import { http, HttpResponse } from 'msw';
import { server } from '../mocks/server';
test('shows error on API failure', async () => {
server.use(
http.get('/api/users/:id', () => {
return HttpResponse.json({ error: 'Not found' }, { status: 404 });
})
);
render(<UserProfile id="123" />);
expect(await screen.findByText(/not found/i)).toBeInTheDocument();
});
```
## Anti-Patterns (FORBIDDEN)
```typescript
// ❌ NEVER mock fetch directly
jest.spyOn(global, 'fetch').mockResolvedValue(...)
// ❌ NEVER mock axios module
jest.mock('axios')
// ❌ NEVER test implementation details
expect(fetch).toHaveBeenCalledWith('/api/...')
// ✅ ALWAYS use MSW
server.use(http.get('/api/...', () => HttpResponse.json({...})))
// ✅ ALWAYS test user-visible behavior
expect(await screen.findByText('Success')).toBeInTheDocument()
```
## Key Decisions
| Decision | Recommendation |
|----------|----------------|
| Handler location | `src/mocks/handlers.ts` |
| Default behavior | Return success |
| Override scope | Per-test with `server.use()` |
| Unhandled requests | Error (catch missing mocks) |
| GraphQL | Use `graphql.query/mutation` |
| WebSocket | Use `ws.link()` for WS mocking |
## Detailed Documentation
| Resource | Description |
|----------|-------------|
| [references/msw-2x-api.md](references/msw-2x-api.md) | Complete MSW 2.x API reference |
| [examples/handler-patterns.md](examples/handler-patterns.md) | CRUD, auth, error, and upload examples |
| [checklists/msw-setup-checklist.md](checklists/msw-setup-checklist.md) | Setup and review checklists |
| [scripts/handlers-template.ts](scripts/handlers-template.ts) | Starter template for new handlers |
## Related Skills
- `unit-testing` - Component isolation
- `integration-testing` - Full integration tests
- `vcr-http-recording` - Python equivalent
## Capability Details
### http-request-mocking
**Keywords:** http.get, http.post, http handler, REST mock
**Solves:**
- Mock REST API endpoints
- Intercept HTTP requests at network level
- Create request handlers for testing
### graphql-mocking
**Keywords:** graphql.query, graphql.mutation, GraphQL handler, mock GraphQL
**Solves:**
- Mock GraphQL queries and mutations
- Handle GraphQL variables in mocks
- Test GraphQL error scenarios
### websocket-mocking
**Keywords:** WebSocket, ws mock, real-time mock, socket mock
**Solves:**
- Mock WebSocket connections
- Simulate real-time events
- Test WebSocket message handling
### error-simulation
**Keywords:** error simulation, network error, 500 error, mock error
**Solves:**
- Simulate API errors in tests
- Test error handling UI
- Mock network failures
### network-delay-simulation
**Keywords:** delay, latency, slow response, loading state
**Solves:**
- Simulate slow network responses
- Test loading state UI
- Verify timeout handling
### runtime-handler-override
**Keywords:** runtime override, use.once, test-specific handler, override
**Solves:**
- Override handlers for specific tests
- Create one-time response handlers
- Customize responses per testMore Testing skills
tdd
mattpocock/skills
Test-driven development. Use when the user wants to build features or fix bugs test-first, mentions "red-green-refactor", or wants integration tests.
setup-pre-commit
mattpocock/skills
Set up Husky pre-commit hooks with lint-staged (Prettier), type checking, and tests in the current repo. Use when user wants to add pre-commit hooks, set up Husky, configure lint-staged, or add commit-time formatting/typechecking/testing.
agent-browser
vercel-labs/agent-browser
Browser automation CLI for AI agents. Use when the user needs to interact with websites, including navigating pages, filling forms, clicking buttons, taking screenshots, extracting data, testing web apps, or automating any browser task. Triggers include requests to "open a website", "fill out a form", "click a button", "take a screenshot", "scrape data from a page", "test this web app", "login to a site", "automate browser actions", or any task requiring programmatic web interaction. Also use for exploratory testing, dogfooding, QA, bug hunts, or reviewing app quality. Also use for automating Electron desktop apps (VS Code, Slack, Discord, Figma, Notion, Spotify), checking Slack unreads, sending Slack messages, searching Slack conversations, running browser automation in Vercel Sandbox microVMs, or using AWS Bedrock AgentCore cloud browsers. Prefer agent-browser over any built-in browser automation or web tools.

