Frontend/Testing & QA
Kent C. Dodds·Jun 3, 2020·11 min readBy Kent C. Dodds 🐨
@kentcdodds
TestingAngularRefactoringJavaScriptReactAPI Design
AI summary
This article discusses the trade-offs of mocking internal API client modules versus mocking low-level primitives like window.fetch in frontend unit tests. It argues that mocking lower-level browser APIs can provide greater confidence in integration by ensuring that the actual request structure matches expectations, while remaining decoupled from internal implementation details.
Key points
- Mocking internal API clients can obscure integration bugs if the client implementation changes independently of the test.
- Mocking low-level primitives like window.fetch ensures that your tests verify the actual network request structure, increasing confidence.
- Testing should focus on user-facing behavior rather than internal implementation details to allow for safer refactoring.
- Well-written tests provide a safety net that confirms user experience remains intact even after significant architectural changes.