Tools: Free Msw-fetch-mock: Undici-style Fetch Mocking For Msw 2026
Posted on Mar 3
• Originally published at recca0120.github.io
When writing frontend or Node.js tests, mocking HTTP requests is practically mandatory. But choosing a mock solution is overwhelming: MSW, nock, fetch-mock, jest-fetch-mock… each has a different API style, interception level, and environment support. If you work on both Cloudflare Workers and Node.js projects, you'll find the mock APIs are completely different and test code can't be shared.
msw-fetch-mock solves exactly this: it provides the same API style as undici's MockAgent and Cloudflare Workers' fetchMock, with MSW handling network-level interception underneath. One API, every environment.
Here are the 6 mainstream HTTP mock solutions today:
msw-fetch-mock doesn't build a mock engine from scratch. It stands on MSW's shoulders — using MSW for network-level interception (Service Worker in browser, @mswjs/interceptors in Node) — and wraps it with an undici-style API.
The key is the single catch-all handler. MSW's standard approach registers one handler per endpoint, but in browser environments this causes Service Worker timing issues (each worker.use() requires SW communication). msw-fetch-mock registers just one http.all('*', ...) catch-all, running all matching logic in the main thread, avoiding Service Worker round-trip latency.
The interception level determines how closely mock behavior matches production:
msw-fetch-mock has three concrete advantages over the alternatives:
The APIs for undici MockAgent, Cloudflare Workers fetchMock, and msw-fetch-mock are nearly identical:
If your code runs on both Node.js and Cloudflare Workers, your test mocks can share the same patterns — just change the import.
Source: Dev.to