MSW 2 adopts Web Standard APIs for its handler system, bringing WebSocket support and a cleaner handler model. Test your understanding of MSW v2's patterns and how they differ from v1.
0 / 5 completed
1 / 5
In MSW 2, what is the correct way to define a GET request handler?
MSW 2 switched to the http namespace and uses the standard Response/HttpResponse APIs instead of the custom res(ctx.*) builder. Handlers now return HttpResponse objects that mirror the Web Fetch API, making them more composable and familiar to developers who know native browser APIs.
2 / 5
What does passthrough() do in an MSW 2 handler?
passthrough() is a special handler return value that tells MSW to let the request go through to the real network. It's useful when you want to mock most endpoints but allow specific ones (like analytics or a real auth service) to hit the actual server, without removing or reordering your handlers.
3 / 5
How does MSW 2 handle WebSocket mocking?
MSW 2 added first-class WebSocket support via the ws namespace. You create a ws.link(url) handler and intercept client.addEventListener('message', ...) to mock incoming messages, or call client.send() to push data from the 'server'. This brings the same declarative mocking philosophy to WebSocket-heavy features.
4 / 5
What is the difference between MSW's browser and Node.js integration modes?
MSW browser mode registers a Service Worker that intercepts outgoing fetch/XHR requests at the network level — no monkey-patching. Node.js mode (for Jest/Vitest) intercepts requests by patching the global fetch and using a custom HTTP interceptor, since Service Workers don't exist in Node. The same handler definitions work in both modes.
5 / 5
In MSW 2, how do you access the request body in a POST handler?
MSW 2 handlers receive a standard Web API Request object, so you access the body using the same methods as the Fetch API: await request.json(), await request.text(), or await request.formData(). This is a key improvement over MSW v1's custom req.json() helper — the same code works in service workers and Node interceptors.