During the past weeks, I've seen posts around the library MSW (MockServiceWorker).
Seamless REST/GraphQL API mocking library for browser and Node.
MSW seems to gain some popularity fast, and most of it is coming from the React community. It even became the recommended approach to mock HTTP requests with React Testing Library. Kent C. Dodds wrote a blog post "Stop mocking fetch" in which he explains what problems MSW can resolve. If you're not familiar with MSW, I would suggest skimming through the documentation or to read Kent's blog before continuing.
Because it receives a lot of positive feedback I wanted to give this a shot with Angular. In this post, we'll create a proof of concept to search a GitHub user for a username.
The implementation of the Angular service, looks as this:
If you want to test a component using the
GitHubService, typically a mocked service instance is provided.
The first step is to install MSW with the following command.
This will create the
./src/mockServiceWorker.js file which includes the Service Worker.
To be able to serve the service worker, add it to the
assets inside the
With this step, the
mockServiceWorker.js file will be copied over to the build output.
That's all for the configuration, now it's time to create the mocked server.
The last part is to import the mock. We're adding the mock to the environment file so it won't be included in a production build.
This gives us the following result.
Because Angular uses a different builder during tests, we also need to add the
mockServiceWorker to the test builder.
Just like when the application is served, the mock server needs to be imported to register the service worker.
We import the mocks in the
./src/test.ts file where the test environment is created, so it's available for all tests.
Now, we can simply write the test without having to provide a mocked instance of the service.
For Jest, we can't use the current setup. Don't worry, the mocks are reused but because Jest runs in a Node environment, the worker can't be.
To reuse the mocks, move it to another file so it's possible to share the setup between a browser environment and a Node environment.
Next, import the mock handlers while setting up the Node server.
Lastly, start the server before each test.
For this, we can use the
The test itself remains the same.
Because Cypress is running tests against the served application, no action is required here. We just have to write the spec.
While Angular already provides a way to mock our services via its dependency injection, I think we can still benefit from MSW in an Angular project.
It doesn't just help to mock services during tests, but as the example setup shows, it can also be used during the development cycle. Especially, when multiple teams (frontend and backend) are working on the same application or when the backend isn't ready yet.
MSW also provides a way to share a server across multiple layers of the testing pyramid. The server can be used during unit tests, integration tests, and end-to-end tests. This can help with the maintenance of your tests.
The code from this post can be found on GitHub.
Edit 2020-08-11: there's now an official MSW Angular REST example
I appreciate it if you would support me if have you enjoyed this post and found it useful, thank you in advance.