MSW Handler Builder
Generate Mock Service Worker v2 request handlers for API mocking in browser and Node.js tests. Configure your endpoint, response, and copy the ready-to-use handler code.
Handler Configuration
0 = no delay. Use to simulate network latency.
Format: Header-Name: value
Generated Handler
Frequently Asked Questions
What is Mock Service Worker (MSW)?▼
Mock Service Worker (MSW) is an API mocking library that intercepts HTTP requests at the network level using a Service Worker in the browser or Node.js interceptors in tests. Unlike mock libraries that patch fetch/axios, MSW intercepts real network requests, making mocks transparent and realistic.
When should I use MSW instead of mocking fetch directly?▼
Use MSW when you want mocks to work across all HTTP libraries (fetch, axios, ky), when you want to test UI components against realistic network behavior, and when you want the same handlers to work in both browser development and Jest/Vitest tests. Direct fetch mocking is brittle and library-specific.
What is the difference between rest and graphql handlers?▼
rest handlers match REST API requests by method and path. graphql handlers match GraphQL operations by operation name regardless of URL. MSW v2 uses the http namespace for REST and graphql namespace for GraphQL operations.
How do I use the generated handler in my project?▼
Install MSW with npm install msw --save-dev, then run npx msw init public/ to register the service worker. Create a handlers.js with your handler, pass it to setupWorker (browser) or setupServer (Node/Jest). Call worker.start() in your app entry or server.listen() in your test setup file.
What does passthrough() do?▼
passthrough() tells MSW to not intercept the request and let it proceed to the real network. This is useful for selectively mocking some endpoints while letting others through to real APIs during development.
Related Products
on GumroadTypeScript Quick Reference Toolkit
5 practical reference files for TypeScript developers: cheatsheet, TS vs JS comparison, 20 utility types with real examples, tsconfig best practices template, and 15 common mistakes guide. Print it. Keep it on your desk.
Buy on Gumroad →DevToolkit Starter Kit
A ready-to-deploy developer tools website with 12 built-in tools. Astro + Preact + Tailwind. Deploy to Cloudflare Pages for free. Built-in SEO, dark mode, and ad-ready layout.
Buy on Gumroad →Developer Productivity Bundle
Stop rebuilding the same setup every project. 51 VSCode snippets, 40 Git aliases, 5 Docker Compose stacks, 5 GitHub Actions workflows, 5 README templates, and a dotfiles kit. Set up a professional dev environment in under an hour.
Buy on Gumroad →You Might Also Need
OpenAPI Validator
Paste your OpenAPI 3.x spec (YAML or JSON) to validate structure, find missing required fields, detect duplicate paths, check for missing operationIds, and get errors and warnings with path references.
Open free tool →
API Response Mock Generator
Generate realistic mock API responses with custom status codes, headers, and JSON body. Quick templates for 200, 401, 404, 429, 500 and more. Runs in your browser.
Open free tool →
API Response Time Tester
Test API endpoint response times directly from your browser. Run multiple requests, see average/min/max times, status codes, and response sizes. GET, POST, PUT, DELETE support. Free.
Open free tool →
More Free Tools
Related Articles
Vitest Unit Testing: Complete Setup Guide for 2026
Learn Vitest from zero to production: installation, test syntax, mocking, coverage reports, Vitest UI, and Vitest vs Jest migration. Practical guide for Vite and non-Vite projects.
API Testing Best Practices 2026: Postman vs Insomnia vs Bruno
Complete guide to API testing in 2026. Compare Postman, Insomnia, and Bruno, learn best practices for REST and GraphQL testing, and build automation pipelines that catch regressions before production.
Test-Driven Development Guide 2026: Practical TDD That Actually Works
Learn TDD the practical way in 2026. Red-green-refactor workflow, real-world examples in JavaScript and Python, when TDD helps vs hurts, and how to introduce it to your team.
Modern JavaScript Testing 2026: Vitest vs Jest vs Playwright
Complete guide to JavaScript testing in 2026. Compare Vitest vs Jest for unit tests, Playwright vs Cypress for E2E, component testing, coverage tools, and CI/CD integration.
Modern TDD in 2026: Test-Driven Development with Vitest, Playwright & AI Copilots
Test-Driven Development has evolved. This practical guide covers modern TDD workflows using Vitest for unit tests, Playwright for E2E, MSW for API mocking, and how AI coding assistants (GitHub Copilot, Claude Code) are changing how we write tests first.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$9/mo after trial · Cancel anytime
Want the full toolkit?
Get DevPlaybook Pro
Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →