The course is part of this learning path
This module looks at testing in React. You’ll learn how to set up a test environment, Jest, and other useful tools to test your React App.
The objectives of this module are to provide you with an understanding of:
- How to set up the test environment
- Snapshot testing
- How to test with Props
- How to mock components for testing
- Mock functions
- How to test components asynchronously
- How to test components with routing
- How to test custom hooks
This learning path is aimed at all who wish to learn how to use the ReactJS framework.
We welcome all feedback and suggestions - please contact us at firstname.lastname@example.org to let us know what you think.
If we think about unit testing, the app can appear to pose us some problems. Components inherently render other components. That could raise difficulties as we depend on the render of these other components to create the tree. And this doesn't sit well with unit testing. We aren't interested in the rendered components' implementation or output as that should be tested independently. However, to maintain the usability of our component, it needs to render something when it's called for.
Jest has a mock function that will help here. The function takes a string that should be the path to the component to mock. The options are as to specify a callback that returns a function. The function will return a JSX expression to represent the component we are mocking in the tree. The Jest mock functions are written at the top of the test code. Any time the unit under test calls for the dependent file, the mock will intercept it and insert the mock code.
Our app component now renders component with props and we've run the test with the original snapshot as the reference. The test has failed. That's because the snapshot generated now renders the new version of the app component with the component with props in it. Pressing U updates the snapshot and the test will pass again.
If we examine the app.test.js.snap file, we can see that the file contains the mock of the component with props. Obviously, this could cause issues if this component changes in the future, and as it receives props, that's highly likely. To get around this, we can mock the component with props in the app.test.js file. Anytime it's needed, it will use this mock implementation and it doesn't matter what the actual component with props does. If that's the case, then what we'll do is mock this component using the Jest mock function.
We'll add the Jest mock function now. It's been supplied with the path to the actual component with props file. We provide a callback that returns a function to represent this mock component. We'll allow it to receive props but just return a simple paragraph that contains the text mocked component with props. Once we save this test, it reruns. We'll need to update the snapshot obviously. If we do that and then check the snapshot, we can see that the mock component has been rendered. Notice the mock component.
So now, we've unitized the app component tests and removed the implementation of the component with props from it. We can implement any number of Jest mock functions so now you can create discrete unit tests for any component that renders other components.
Introduction to Testing React with Jest - How to set up the Test Environment - Jest - The What and How of Testing in React - Snapshot Testing - Testing Components with Props - Testing State Events Interactions - Mocking Functions - Testing Components Asynchronously - Testing Components with Routing - Testing Custom Hooks