Network errors

Loading "Network Errors"
It's time to use API mocking to help us discover blind spots in our getAuthToken() function!
A common mistake when dealing with the network is forgetting about network error handling. Now, I'm not talking about error responses (we covered those in the previous exercise), but rather the scenarios where the request fails entirely.
Here are 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 aren't related to the server response but rather to network connectivity issues. Despite that, we can still model these scenarios using Mock Service Worker!
http.post('/endpoint', () => {
  // Produce a network error for this request.
  return Response.error()
})
The Response.error() static method is the 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 our 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.