There are many types of components that can be unit tested. We have routes, async functions, timer functions, states, redux, lifecycle methods, charts, etc that need to be unit tested.
Why developers don't write unit test case
One of the main reasons the developers do not write test cases is that writing correct and useful test cases is hard. When you look into the examples in test libraries like jest, they are very simple. They test some simple functions. But in reality, when working on real projects, things are not always ideal. You need to write code that is testable. You need to know which code needs to be tested and which code would be better off with snapshot testing or integration testing.
Writing unit test cases for different components in React
In this series of tutorials, we will look into testing all these aspects mentioned above, so that you get a complete picture of unit testing in React. By the end of the tutorial series, you will be a developer who can write test cases easily and can start working in Test Driven Development (TDD) mode.
We are going to look into setting up the environment for jest. Then we will look into writing unit test cases specific to each type of component - example writing unit test case for routes, async functions, etc. Then we will look into different types of testing available in React.
- How to write unit test cases for asynchronous code Axios or fetch library in React using jest and enzyme
- Setting up the environment for writing unit test cases in React using jest
- How to unit test the timer functions – setTimeout and setInterval in React using jest
- How to unit test state of the component using jest and enzyme
- How to test routes (react router dom) in react using jest – 1
- How to test routes (react router dom) in react using jest and memory router – 2
- How to write the unit test cases for events in react using jest and enzyme