
MSW Has a Free API — Mock APIs in the Browser and Node.js
MSW (Mock Service Worker) intercepts network requests at the service worker level. Mock APIs for development, testing, and Storybook — without changing your application code. Why MSW? Network-level mocking — intercepts fetch/XHR, not your code Same handlers — use the same mocks in browser AND tests Storybook integration — mock APIs in stories TypeScript — fully typed request/response handlers Quick Start npm install msw --save-dev npx msw init public/ --save # For browser Handlers // 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/users ' , async ({ request }) => { const body = await request . json (); return HttpResponse . json ({ id : 3 , ... body }, { status : 201 }); }), http . get ( ' /api/users/:id ' , ({ params }) => { return HttpResponse . json ({ id : params . id , name : ' Alice '
Continue reading on Dev.to Tutorial
Opens in a new tab



