Testing React Apps
At Facebook, we use Jest to test React applications.
#
Setup#
Setup with Create React AppIf you are new to React, we recommend using Create React App. It is ready to use and ships with Jest! You will only need to add react-test-renderer
for rendering snapshots.
Run
#
Setup without Create React AppIf you have an existing application you'll need to install a few packages to make everything work well together. We are using the babel-jest
package and the react
babel preset to transform our code inside of the test environment. Also see using babel.
Run
Your package.json
should look something like this (where <current-version>
is the actual latest version number for the package). Please add the scripts and jest configuration entries:
And you're good to go!
#
Snapshot TestingLet's create a snapshot test for a Link component that renders hyperlinks:
Note: Examples are using Function components, but Class components can be tested in the same way. See React: Function and Class Components. Reminders that with Class components, we expect Jest to be used to test props and not methods directly.
Now let's use React's test renderer and Jest's snapshot feature to interact with the component and capture the rendered output and create a snapshot file:
When you run yarn test
or jest
, this will produce an output file like this:
The next time you run the tests, the rendered output will be compared to the previously created snapshot. The snapshot should be committed along with code changes. When a snapshot test fails, you need to inspect whether it is an intended or unintended change. If the change is expected you can invoke Jest with jest -u
to overwrite the existing snapshot.
The code for this example is available at examples/snapshot.
#
Snapshot Testing with Mocks, Enzyme and React 16There's a caveat around snapshot testing when using Enzyme and React 16+. If you mock out a module using the following style:
Then you will see warnings in the console:
React 16 triggers these warnings due to how it checks element types, and the mocked module fails these checks. Your options are:
- Render as text. This way you won't see the props passed to the mock component in the snapshot, but it's straightforward:
- Render as a custom element. DOM "custom elements" aren't checked for anything and shouldn't fire warnings. They are lowercase and have a dash in the name.
- Use
react-test-renderer
. The test renderer doesn't care about element types and will happily accept e.g.SomeComponent
. You could check snapshots using the test renderer, and check component behavior separately using Enzyme. - Disable warnings all together (should be done in your jest setup file):This shouldn't normally be your option of choice as useful warnings could be lost. However, in some cases, for example when testing react-native's components we are rendering react-native tags into the DOM and many warnings are irrelevant. Another option is to swizzle the console.warn and suppress specific warnings.
#
DOM TestingIf you'd like to assert, and manipulate your rendered components you can use react-testing-library, Enzyme, or React's TestUtils. The following two examples use react-testing-library and Enzyme.
#
react-testing-libraryYou have to run yarn add --dev @testing-library/react
to use react-testing-library.
Let's implement a checkbox which swaps between two labels:
The code for this example is available at examples/react-testing-library.
#
EnzymeYou have to run yarn add --dev enzyme
to use Enzyme. If you are using a React version below 15.5.0, you will also need to install react-addons-test-utils
.
Let's rewrite the test from above using Enzyme instead of react-testing-library. We use Enzyme's shallow renderer in this example.
The code for this example is available at examples/enzyme.
#
Custom transformersIf you need more advanced functionality, you can also build your own transformer. Instead of using babel-jest
, here is an example of using @babel/core
:
Don't forget to install the @babel/core
and babel-preset-jest
packages for this example to work.
To make this work with Jest you need to update your Jest configuration with this: "transform": {"\\.js$": "path/to/custom-transformer.js"}
.
If you'd like to build a transformer with babel support, you can also use babel-jest
to compose one and pass in your custom configuration options: