
MSW Has a Free API Mocking Framework — Stop Using Custom Mock Servers
You're probably mocking APIs wrong. Custom mock servers, intercepting fetch globally, hardcoding responses — all fragile, all pain. MSW (Mock Service Worker) fixes this at the network level. What is MSW? MSW intercepts HTTP requests at the network level using Service Workers (browser) or custom interceptors (Node.js). Your application code doesn't know it's being mocked. That's the point. Why MSW Changes Everything 1. Network-Level Interception 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 user = await request . json (); return HttpResponse . json ({ id : 3 , ... user }, { status : 201 }); }), ]; Your fetch() , axios , ky — all intercepted. No code changes needed. 2. Same Mocks for Browser AND Node.js // Browser (Storybook, dev server) import { setupWorker } from '
Continue reading on Dev.to JavaScript
Opens in a new tab



