Mock Service Worker (MSW)
Intercept network requests at the service worker level for seamless API mocking in tests and development.
Overview
Your AI agent can write request handlers using MSW's intuitive API: http.get, http.post, and other methods that mirror your REST endpoints or GraphQL operations. The handlers return mock responses that your components consume exactly as they would real API responses. This means your tests and development environment use the same code, just with different data sources.
MSW is especially powerful for testing because it does not require any changes to your application code. Your AI agent can set up handlers for happy paths, error states, loading states, and edge cases. It can configure MSW for both browser-based Storybook stories and Node.js-based Jest/Vitest tests, providing a unified mocking strategy across your entire test suite.
Who Is This For?
- Frontend developers mocking API responses during development without a backend
- Test engineers creating realistic network scenarios for integration tests
- Teams testing error handling by simulating server failures and timeouts
- Storybook users mocking API dependencies for component stories
Installation
npm install -D msw
npx msw init public/
Claude Code generates request handlers and test setups Configuration
// src/mocks/handlers.ts
import { http, HttpResponse } from "msw";
export const handlers = [
http.get("/api/users", () => {
return HttpResponse.json([
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
]);
}),
http.post("/api/login", async ({ request }) => {
const body = await request.json();
return HttpResponse.json({ token: "mock-jwt-token" });
}),
]; Explore AI Tools
Discover the best AI tools that complement your skills
Read AI & Design Articles
Tips and trends in the world of design and AI
Related Servers
Linear MCP Server
Manage Linear issues, projects, and workflows directly from your AI coding agent without leaving the terminal.
Playwright MCP
Automate browser interactions and run end-to-end tests through the Model Context Protocol, enabling AI agents to verify UI behavior in real browsers.
Jest Test Runner
Run, debug, and analyze Jest test suites directly from your AI agent. Quickly identify failing tests and get suggested fixes.