MSW vs mocking fetch or axios directly in Jest tests

Network interception vs function stubbing.
MSW intercepts HTTP via Service Workers and Node, staying client-agnostic; direct fetch or axios mocks couple tests to specific libraries.
Calling MSW a Jest-only utility.
WHAT THIS TESTS: This question probes whether you understand where to place the abstraction boundary in test infrastructure. Junior engineers often stub the client library they happen to be using; senior engineers intercept at the network layer so the application code under test exercises real request paths. The interviewer wants to hear that MSW is not a testing framework plugin but a standalone network mocking layer that works in both browser and Node environments.
A GOOD ANSWER COVERS: First, the mechanism. In the browser MSW registers a Service Worker that intercepts outgoing requests before they hit the network; in Node it uses request interception to achieve the same effect without a Service Worker. Second, the client agnosticism. Because MSW sits below the HTTP client, it does not matter if the app uses fetch, axios, React Query, or Apollo; the mock responses are identical. Third, the standalone handler layer. Handlers live in a separate file and are imported into tests, Storybook, or E2E suites, creating a single source of truth for network behavior. Fourth, the realism advantage. Integration and E2E tests run against actual production request code, so you catch header mismatches, serialization bugs, and client configuration errors that function-level mocks hide.
COMMON WRONG ANSWERS: Saying MSW is a Jest utility or that it replaces jest.mock for modules. Claiming it only works in the browser and has no Node support. Arguing that mocking fetch directly is simpler without acknowledging the maintenance cost when the team switches HTTP clients or upgrades versions. Suggesting MSW is overkill for unit tests without explaining that the same handlers are reused in E2E tests, which amortizes the setup cost.
LIKELY FOLLOW-UPS: How do you handle network errors or timeouts with MSW? How would you share handlers between a React app and a React Native app? What is the difference between setupWorker and setupServer? How do you bypass certain requests so they hit the real API? Can MSW mock WebSocket or Server-Sent Events traffic?
ONE CONCRETE EXAMPLE: Imagine a checkout flow that uses axios in the web app but fetch in a new micro-frontend. If you mocked axios.get in Jest, the micro-frontend tests would need entirely new mocks and the integration suite would hit the real API or require yet another stubbing strategy. With MSW, you define a POST handler for the checkout endpoint once. The web app tests, the micro-frontend tests, and the Playwright E2E suite all import the same handlers. When the team migrates from axios to the native fetch, zero test code changes because the mocks live at the network boundary, not inside the client wrapper.
Source: mswjs.io
Read the original → mswjs.io
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.