Skip to main content

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.

✓ MSW v2 syntax (http namespace) ✓ REST & GraphQL handlers ✓ Custom status, headers, body ✓ Delay simulation support

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.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

OpenAPI Validator API Response Mock Generator API Response Time Tester OpenAPI Schema Validator OpenAPI Mock Generator API Test Case Generator API Request Builder JS Prototype Chain Visualizer API Mock Server Config Generator Jest Testing Framework Guide API Rate Limit Calculator API Response Formatter & Analyzer HTTP API Tester JavaScript Formatter & Minifier LLM Cost Calculator AI Prompt Cost Calculator REST API Request Builder Vitest Guide — Vite-native Testing Cypress Frontend Testing Guide Mock Service Worker (MSW) API Mocking Guide LLM Prompt Cost Calculator API Response Mocker AI Rate Limit Calculator HTML5 Canvas API Playground API Key Generator AI Doc Generator — Pro Tool AI Unit Test Generator — Pro Tool Code Tools HTTP Methods Reference HTTP Code Snippet Generator HTTP Status Code Reference HTTP Status Code Reference JSON Diff Viewer JSON Diff Prettier Config Generator Regex Visualizer Regex Tester Pro Regex Tester TypeScript Playground TypeScript to JavaScript Converter Rate Limiter Tester API Changelog Generator HTTP Mock Response Builder Rate Limit Calculator Biome Config Generator CORS Policy Builder HTTP Status Code Lookup HTTP Status Code Lookup Vue Component Generator HTTP Status Code Reference LLM Context Window Calculator Regex Flags Explainer LLM Token Counter k6 Load Test Script Generator Playwright E2E Test Generator Bundle Size Estimator JavaScript Memory Leak Detector Prompt Token Estimator NPM Bundle Size Estimator Playwright E2E Testing Guide Pulumi Modern IaC Guide Bundle Size Impact Calculator Game Loop Calculator Bezier Curve Editor Particle System Config Generator Prompt Injection Tester Token Context Calculator OpenAI Function Calling Generator Token Counter & Cost Estimator AI API Rate Limit Calculator

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$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.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →