Network errors

Loading "Network Errors"
It's time to use API mocking to help us discover blind spots in our getAuthToken() function!
One of the common mistakes when dealing with the network is forgetting about network error handling. Now, I'm not talking about error responses (we've handled those in the previous exercise) but about the scenarios when the request fails altogether.
Here's a few examples of network errors:
  • The requested server is down;
  • The client goes offline or the server connection is interrupted;
  • The client performs an invalid request.
These errors have nothing to do with the server response but rather with the network connectivity. Despite that, we can still model those scenarios using Mock Service Worker!
http.post('/endpoint', () => {
  // Produce a network error for this request.
  return Response.error()
})
The Response.error() static method is a standard API for creating network errors primarily designed to be used in Service Workers.
In the request handler above, MSW will respond to POST /endpoint requests with a network error. That, in turn, will reject the response promise and allow us to test how well we handle that scenario in code.
πŸ‘¨β€πŸ’Ό Use what you've learned about the runtime request handlers (server.use()) to complete the network error handling test case in .
πŸ‘¨β€πŸ’Ό Once you can reproduce the network error in the test, implement the error handling in (handle the fetch() promise rejections) and add the respective assertions in the new test case. Verify your solution by running npm test.

Access Denied

You must login or register for the workshop to view and run the tests.

Check out this video to see how the test tab works.