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 that 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 app that we are going to work with now resides in start/apps/chapter10/ng-jest-global-mocks
inside the cloned repository:
- Open the code repository in your code editor.
- Open the terminal, navigate to the code repository directory, and run the following command to serve the project:
npm run serve ng-jest-global-mocks
This should open the app in a new browser tab, and you should see the following...