Mocking API calls with MSW
In the preceding recipe, we have seen how Angular’s HttpTestingController
class helped us mock integration with external services and prevent real HTTP requests from being triggered.
In this recipe, we’re going to explore how we can simplify this process even further with the Mock Service Worker (MSW) library. In general, MSW is like a proxy between the browser and external services, so it is ideal to intercept all ongoing requests and return what we desire instead of pinging the real backend service. This is especially useful for writing integration tests.
Getting ready
In this recipe, we’re going to test network request services that we implemented in Chapter 1 (https://github.com/PacktPublishing/RxJS-Cookbook-for-Reactive-Programming/tree/main/Chapter01/network-requests).
How to do it…
Since MSW works only with Jest, we first need to set up the test configuration in the Angular project to use Jest, and not...