📎 Webclip
Stop mocking fetch
The article argues that mocking a client or window.fetch can hide bugs in request shape, headers, and auth. It also adds duplicated backend setup across tests and reduces confidence in integration-level behavior.
Reading notes#
- Mocking the client makes it harder to know whether the client is being used correctly, especially if the client API changes.
- Mocking
window.fetchgives more confidence than mocking the client, but still leaves gaps around headers and authentication details. - Reimplementing backend behavior inside tests creates setup noise and duplicate work.
- A single fetch mock function can simulate backend behavior and keep most happy-path tests simple.
- MSW intercepts requests and lets tests use server handlers that resemble the real backend.
- The same handlers can be reused in browser development, which helps when endpoints are missing, broken, or unreachable.
- The article suggests colocating only the test-specific parts and using runtime handlers for edge cases and failures.
