React Testing Library helps us write good tests and makes writing bad tests difficult. It allows us to find elements in a way similar to how a user would do that: for example, find form elements and buttons by their labels.
The best location for a test is close to the source code. For example, if we have a component at src/components/Button.js, a test for this component could be at src/components/__tests__/Button.spec.js. Jest will find and run this file automatically.
Here we’re rendering a paragraph of text using the Testing Library’s render() method, then testing that a paragraph containing “Hello Jest!” is present on the page using Testing Library’s getByText() method and jest-dom’s toBeInTheDocument() matcher.
We’re also using the screen namespace to access the queries.
Run npm test (or npm t) to run all tests. We should see something like this:
Run npm run test:watch to run Jest in watch mode: Jest will run only tests that are related to files changed since the last commit, and Jest will rerun these tests any time we change the code. This is how I usually run Jest. Watch mode is fast enough even on large projects, where running all tests takes many minutes.
Run npm run test:coverage to run all tests and generate coverage report in the coverage folder.
Jest snapshots work like this: we tell Jest that we want to be sure that output of this component should never change accidentally and Jest saves the component output, called a snapshot, to a file:
Every time someone changes the markup, Jest shows a diff and asks whether to update a snapshot, if the change isn’t accidental.
We can use snapshots to store any values: React tree, strings, numbers, object, and so on
Snapshot testing sounds like a good idea, but has several problems:
easy to commit snapshots with bugs;
failures are hard to understand;
a small change can lead to hundreds of failed snapshots;
we tend to update snapshots without thinking;
coupling with low-level modules;
test intentions are hard to understand;
they give a false sense of security
they don’t prove that the app work or even look as exepected.
Avoid snapshot testing except testing short output with clear intent, like class names or error messages, or when we really want to verify that the output is the same.
Tests should resemble how users interact with the app. That means we shouldn’t rely on implementation details because the implementation can change and we’ll have to update our tests. This also increases the chance of false positives when tests are passing but the actual feature is broken.
Let’s compare different methods of querying DOM elements:
Selector
Recommended
Notes
button
Never
Worst: too generic
.btn.btn-large
Never
Bad: coupled to styles
#main
Never
Bad: avoid IDs in general
[data-testid="cookButton"]
Sometimes
Okay: not visible to the user, but not an implementation detail, use when better options aren’t available
[alt="Chuck Norris"], [role="banner"]
Often
Good: still not visible to users, but already part of the app UI
[children="Cook pizza!"]
Always
Best: visible to the user part of the app UI
To summarise:
Text content may change and we’ll need to update our tests. This is good: we want our test to work with the actual text users see in the app.
Test IDs clutter the markup with props we only need in tests. Test IDs are also something that users of our app don’t see: if we remove a label from a button, a test with test ID will still pass.
Testing Library has methods for all good queries, they are called sematic queries. There are six variants of query methods:
getBy*() returns the first matching element and throws when an element not found or more than one element found;
queryBy*() returns the first matching element but doesn’t throw;
findBy*() returns a promise that resolves with a matching element, or rejects when an element not found after a default timeout or more than one element found;
getAllBy*(), queryAllBy*(), findAllBy*(): same as above but return all found elements, not just the first one.
getByLabelText() finds a form element by its <label>;
getByPlaceholderText() finds a form element by its placeholder text;
getByText() finds an element by its text content;
getByAltText() finds an image by its alt text;
getByTitle() finds an element by its title attribute;
getByDisplayValue() finds a form element by its value;
getByRole() finds an element by its ARIA role;
getByTestId() finds an element by its test ID.
Each query is available in all variants. For example, besides getByLabelText() there are also queryByLabelText(), getAllByLabelText(), queryAllByLabelText(), findByLabelText() and findAllByLabelText().
Let’s see how to use query methods. To select this button in a test:
We can either query it by its test ID:
Or query it by its text content:
Note the regular expression (/cook pizza!/i) instead of a string literal ('Cook pizza!') to make the query more resilient to small tweaks and changes in the content.
Or, the best method, query it by its ARIA role and label:
Benefits of the last method are:
doesn’t clutter the markup with test IDs, that aren’t perceived by users;
doesn’t give false positives when the same text is used in non-interactive content;
makes sure that the button is an actual button element or at least have the button ARIA role.
These tests can be useful when our component has several variations and we want to test that a certain combinnation of props renders the correct variation.
Here we’re testing that our Pizza component renders all ingredients passed to a component as a prop.
To simulate user interaction, like clicking or typing, use user-event methods, and then test the output:
Here, we have a component that shows some text on the “Expand” button click, and hides it on the “Collapse” button click. Our test verifies this behavior.
We’re using queryByText() method instead of getByText() because the former doesn’t throw when an element can’t be found, so we can test that an element doesn’t exist.
See the next section for a more complex example of testing events.
When we unit test a single component, event handlers are often defined in the parent component, and there are no visible changes as a reaction to these events. They also define the API of a component that we want to test.
jest.fn() creates a mock function, or a spy, that allows us to check how many times it was called and with which parameters.
Here, we’re using jest.fn() to define a spy for the onSubmit prop of our Login component, then we’re filling the form by typing text into input fields, then we click the submit button, and check that the onSubmit function was called only once and it has received login and password.
In comparison to Enzyme, we don’t have to call a form submit handler directly, which would be testing the implementation. User-event’s click() method will dispatch a click event on the DOM node which is captured and handled by React the same way a real click in a browser would be handled. For example, it will dispatch a form submit event when we “click” a <button type="submit">, and won’t dispatch it when we “click” a <button type="button">, which makes our tests more reliable.
Asynchronous operations are the most tricky to test. Often developers give up and add random delays to their tests:
This approach is problematic. The delay will always be a random number. A number that is good enough on a developer’s machine at the time of writing the code. But it can be too long or too short at any other time and in any other environment. When it’s too long, our tests will run longer than necessary. When it’s too short, our tests will break. This makes tests flaky.
A better approach would be polling: waiting for something, like a new text on a page, by checking it multiple times with short intervals, until it’s there. Testing Library has the waitFor() method:
For querying elements we can use findBy*() and findAllBy*() methods that could wait for an element to appear:
Now our tests wait as long as necessary but not more.
There are many ways to test components that send network requests:
dependency injection;
mocking a service module;
mocking a high-level network API, like fetch;
mocking a low-level network API, that catches all ways of making network requests;
starting a mock server and sending requests to this server instead of a real API.
I’m not mentioning sending a real network request to a real API as an option here, because it’s slow and fragile. Every network problem or change of the data, returned by the API, may break our tests. Also, we’ll need to have the right data for all test cases — hard to achieve with a real API or a database.
Let’s look at some of the methods in more detail.
Dependency injection is when we pass a dependency as a function parameter or a component prop, instead of hardcoding it inside of a module. This allows us to pass another implementation in a test. Use default function parameters or default component props to define the default implementation, one that should be used in non-test code. That way we don’t have to pass the dependency every time we use a function or a component:
When we use our component without passing the fetchIngredients prop, it’ll use the default implementation, that makes an actual network request:
But in a test we’re passing a custom implementation, that returns mock data instead:
Dependency injection is great for unit tests, when we’re rendering a component that accepts an injection directly, but for integration tests it needs too much boilerplate to pass dependencies to deeply nested components.
That’s where request mocking comes in.
Mocking is similar to dependency injection in a way that we’re also replacing a dependency implementation with our own in a test but it works on a deeper level: by modifying how either module loading or browser APIs, like fetch, work.
With jest.mock() we can mock any JavaScript module. To make it work in our case, we need to extract our fetching function to its own module, often called a service module:
We’re using Jest’s mockResolvedValue method to resolve a Promise with a mock data.
Mocking the network is similar to mocking a method, but instead of importing a method and mocking it with jest.mock(), we’re matching a URL and giving a mock response. And it works on the lowest level, so network requests, sent using fetch or XMLHttpRequest, will be mocked.
We’ll use Mock Service Worker to mock the network requests. It works in the browser and in Node.js, which makes it possible to reuse the same mocks in unit tests, React Styleguidist or Storybook.
To setup Mock Service Worker, we need to define mock handlers for all our network endpoints:
Here we’re starting the mock server before running the first test and stopping it after running the last one. We’re also resetting mock handlers before each test, so we could customize handlers in a particular test to verify different scenarios, for example, and error case.
To summarize the difference between jest.mock() and Mock Service Worker:
jest.mock() is already available with Jest and we don’t need to set up and learn anything new — it works the same way as mocking any other module, however it dependes on the implementation of service modules and may break tests when the implementaiton changes.
Mock Service Worker has a specialized API to describe network requests and responses, and debugging tools to help us when something isn’t working. It’s easier to customize responses for particular tests. It also works the same way in the browser and Node.js so we could reuse mocks.
We’ve learned how to set up React Testing Library and how to test different React components. We’ve learned how to setup Mock Service Worker, and use it to test components that make network requests.
Thanks to Joe Boyle, Kent C. Dodds, Anna Gerus, Patrick Hund, Monica Lent, Morgan Packard, Alexander Plavinski, Giorgio Polvara, Juho Vepsäläinen.