How to set up the Test Environment
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.
- [Instructor] If you've started a project using create-react-app. You've already got everything you need to start testing your application setup. It comes with jest baked into the setup and a sample test spec for the app component. If you examine the package.json file, the command test is already in the script section on line 15. And you can see that testing library jest-dom, react and user-event have been added as dependencies, on line six to eight. This is the package.json file from a new app scaffolded with create-react-app. The command npm test can be used on the console to run the tests. And these will hot load as you make changes to them or the files they test. We press A to run all of the tests, and you can see that the default test provided for the app component passes. Tests should have .test. in their file name as a convention, to help identify them. But create-react-app is to configured, to look for tests in any file and to use them when running tests. For this reason, the test files don't need to be saved in the same folder as the item they were testing. In fact it could get mighty confusing, if you don't follow the convention. We've moved the app.test.js file into a folder we've called __testing __. Those underscores help to visually identify it as non-production code folder. And there's another convention. If you haven't used create-react-app. The react and jest documentation explain how to add testing to your project.
Introduction to Testing React with Jest - Jest - The What and How of Testing in React - Snapshot Testing - Testing Components with Props - Mocking Components for Testing - Testing State Events Interactions - Mocking Functions - Testing Components Asynchronously - Testing Components with Routing - Testing Custom Hooks
Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.
Ed is responsible for delivering QA’s Programming Foundations course using the Eclipse IDE. His skillset extends into the DevOps sphere, where he is able to deliver courses based around Agile/Scrum practices, version control, and CI/CD.