Providing global mocks for Jest
In the previous recipe, we learned how to set up Jest for Angular unit tests. There might be some scenarios in which you'd want to use a browser API that might not be part of your actual Angular code; for instance, using localStorage
or alert()
. In such cases, we need to provide some global mocks for the functions we want to return mock values from. This is so that we can perform tests involving them as well. In this recipe, you'll learn how to provide global mocks to Jest.
Getting ready
The project for this recipe resides in chapter10/start_here/providing-global-mocks-for-jest
. Perform the following steps:
- Open the project in Visual Studio Code.
- Open the Terminal and run
npm install
to install the dependencies of the project. - Once done, run
ng serve -o
.
This should open the app in a new browser tab. The app should appear as follows: