DEV Community

AlaminHossain01052000
AlaminHossain01052000

Posted on

Unit test in React

We don’t give so much interest in unit testing but it is important. It allows us to find the bug and efficiently debug them.
We do not have to test all the components of a React Js project.
Suppose, we have a website like Amazon. The main feature of that, users can see the products and can purchase them. So if we provide testing in all the components it is good but not the better option. My code and project will be fine if I implement unit testing only on those features.
To do a default unit test of React Js firstly I have to create a file named components.test.js.
Then in that file, I have to invoke an inbuilt method test and pass two arguments (the first one is a statement and the second one is a function).
In the second argument, I have to render the component I want to test and then write the screen. debug().
Finally when I input the command npm test and then our test will run.
Thus we can test a React.Js project
We have to remind in our mind if a feature is not so important but any other component relies on it just like useFirebase hook. The information of the user is important in many components. If any bug exists in it then it will hamper many components and all of the projects. So to avoid bugs we can apply I simple unit test in that component

Top comments (0)