Jest Plugin
Jest is an open source test runner created by Facebook. It has a lot of great features:
- Immersive watch mode for providing near instant feedback when developing tests.
- Snapshot testing for validating features.
- Great built-in reporter for printing out test results.
Setting up Jest
By default, Nx will use Jest when creating applications and libraries.
1nx g @nrwl/web:app frontend
Adding Jest to an Existing Project
Add Jest to a project using the jest-project
generator from @nrwl/jest
.
First, install @nrwl/jest
, if not already installed using your preferred package manager.
1npm install --save-dev @nrwl/jest
1yarn add --dev @nrwl/jest
Once installed, run the jest-project
generator
1nx g @nrwl/jest:jest-project --project=<project-name>
Hint: You can use the
--dry-run
flag to see what will be generated.
Replacing <project-name>
with the name of the project you're wanting to add Jest too.
Using Jest
Testing Applications
Run the Jest test with
1nx test frontend
Watching for Changes
Using the --watch
flag will run the tests whenever a file changes.
1nx test frontend --watch
Snapshot Testing
Jest has support for Snapshot Testing, a tool which simplifies validating data. Check out the official Jest Documentation on Snapshot Testing.
Example of using snapshots:
1describe('SuperAwesomFunction', () => {
2 it('should return the correct data shape', () => {
3 const actual = superAwesomFunction();
4 expect(actual).toMatchSnapshot();
5 });
6});
When using snapshots, you can update them with the --updateSnapshot
flag, -u
for short.
By default, snapshots will be generated when there are not existing snapshots for the associated test.
1nx test frontend -u
Snapshot files should be checked in with your code.
Configurations
Jest
Primary configurations for Jest will be via the jest.config.js
file that generated for your project. This file will extend the root jest.config.js
file. Learn more about Jest configurations.
Nx
Nx Jest Plugin options can be configured via the project config file or via the command line flags.
Hint: Use
--help
to see all available options1nx test <project-name> --help
Code Coverage
Enable code coverage with the --coverage
flag or by adding it to the executor options in the project configuration file.
By default, coverage reports will be generated in the coverage/
directory under projects name. i.e. coverage/apps/frontend
. Modify this directory with the --coverageDirectory
flag. Coverage reporters can also be customized with the --coverageReporters
flag.
coverageDirectory
andcoverageReporters
are configurable via the project configuration file as well.
Debugging Failing Tests
If your code editor doesn't provide a way to debug your tests, you can leverage the Chrome DevTools to debug your tests with the --inspect-brk
flag for node.
1node --inspect-brk ./node_modules/@nrwl/cli/bin/nx test <project-name>
Enter chrome://inspect in Chrome address bar and inspect the target to attach to the node process. Visit the official Jest documentation to find out more.