Courses / Frontend Testing
10. Mocking fetch and XHR
Stubbing network requests in tests using global.fetch mocks or libraries like msw.
1 min read Chapter 10 of 25
JavaScript
Mocking fetch and XHR
Replacing real network calls in tests with controllable fake responses so tests run fast and deterministically.
"I don't want my tests to depend on a flaky external API or network speeds."
Mocking fetch/XHR means intercepting code that makes HTTP requests and returning predefined responses so tests can assert behavior without real network traffic.
"I don't want my tests to depend on a flaky external API or network speeds."
Mocking fetch/XHR means intercepting code that makes HTTP requests and returning predefined responses so tests can assert behavior without real network traffic.