React-testing-library have fantastic testing
Tags: javascript, frontend, testing, learning
Published: 2019-04-02 21:19:20

---
title: React-testing-library have fantastic testing
published: true
tags: javascript,frontend,testing,learning
canonical_url: https://medium.com/yazanaabed/react-testing-library-have-a-fantastic-testing-198b04699237
---

_I did not know what is testing, and why people check their implementation. So, I decided to learn more about it, and the favour is for_ [_Kent C. Dodds_](https://medium.com/u/db72389e89d8) _testing courses._

_Check my other article about testing, and what I know about testing for more information_ [_“Make JavaScript Testing Work for You”_](https://medium.com/yazanaabed/what-i-know-about-testing-in-javascript-6657d2188c3a)_._

![](https://cdn-images-1.medium.com/max/1024/0*NZHHyLwNW_0dS3HE)<figcaption>Photo by <a href="https://unsplash.com/@siniz?utm_source=medium&amp;utm_medium=referral">Siniz Kim</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption>

You can follow me on [twitter](https://twitter.com/YazanAabed) or check my latest articles on [my site yaabed.com](https://www.yaabed.com/). Also, I have my publication at [medium blog.yaabed.com](https://medium.com/yazanaabed).

A library is something that contains things you do it every day like jQuery includes an implementation for dealing with DOM element efficiently.

Testing is something cool in programming phases, I don’t know why people hate it. It gives you the confidence that your code never broke after a while, typically when the application going big every day, and new people joining your team.

> _Your test must make your code better. — _[_Kent C. Dodds_](https://medium.com/u/db72389e89d8) [_workshop_](https://frontendmasters.com/courses/testing-react/)_._

So, what is react-testing-library? And why [Kent C. Dodds](https://medium.com/u/db72389e89d8) built it for us? Let’s start by defining what is react-testing-library and how to use it.

React-testing-library is a small library change a way you deal with a DOM element, I mean instead of searching for a div element as a result from your component, this library gives you the ability to search for things using the final result, like what user sees in his browser, users see texts, not divs.

When you search for a text, this will give you the confidence that you are not testing the implementation details for your component. For example, if you have a header or navbar for your application and when the user logged in your header component need to change the text from login to be a username, react-testing-library make it easy for us.

What is the benefit of 100% testing coverage and the user for your application can’t even be logged in? For me, I didn’t get the chance to work with a team that tests their code. I hope this will be soon, I learn testing with my self and watch courses about this topic.

Check this example how things broke when you change the selector for your testing element.

![](https://cdn-images-1.medium.com/max/1024/1*uV-Ndde361wFQNQEI8813g.png)

![](https://cdn-images-1.medium.com/max/1024/1*6Ozt07wyOekoI2_hjvosXQ.png)

If by mistake or by the requirement one of your team member change the class selector from “login-button” to something else, this will break your testing and need to change the test every time the selector changed.

To solve this problem, we can use react-testing-library to solve the selector problem by searching for login text inside the component.

![](https://cdn-images-1.medium.com/max/1024/1*izrhxvkl5CdidapdOuV7Hw.png)

For me, the second example is more comfortable to understand that you are searching for a login button and you want to click it. In that example, you will simulate the user behaviour.

The entry point is the render method from react-testing-library, it returns various utils, and you can use it with your tests.

There are also multiple entry points you can use it, check the [Github](https://github.com/kentcdodds/react-testing-library) account for this library for more information.

In the above example, the render method returns getByText util, and there are more and more:

- [container](https://github.com/kentcdodds/react-testing-library/blob/fd2df8d18652786a95bce34741180137f9d2cef2/README.md#container): This returns a regular DOM element that used to contain the rendered component.
- [getByLabelText](https://github.com/kentcdodds/react-testing-library/blob/fd2df8d18652786a95bce34741180137f9d2cef2/README.md#getbylabeltexttext-textmatch-options-selector-string---htmlelement): This will be used to get a form element that its label is the input text.
- [getByTestId](https://github.com/kentcdodds/react-testing-library/blob/fd2df8d18652786a95bce34741180137f9d2cef2/README.md#getbytestidtext-textmatch-htmlelement): This will get an element by its data-testid attribute.

Another util is the [wait](https://github.com/kentcdodds/react-testing-library/blob/fd2df8d18652786a95bce34741180137f9d2cef2/README.md#wait) method allows you to wait for a time, for example an API call. I suggest you give it a try by your self it is fantastic.

Like if my login button trigger a login API to make sure that this is the right user, this change will break our test because the getByText will through an exception that it is not finding the element. So to solve this we will use the wait method from react-testing-library, it will wait until the logout text appears on the page.

But, for an API call, please use something called mock with jest, I gave an introduction to it in [my other article](https://medium.com/yazanaabed/what-i-know-about-testing-in-javascript-6657d2188c3a).

Also, don’t forget to clear your tests before each test, you can use abeforeEach method in jest or you can use react-testing-library util called a cleanup-after-each. You just need to import it from the library like this: import “react-testing-library/cleanup-after-each”;

![](https://cdn-images-1.medium.com/max/1024/1*c26CycmIpP8vIAZMMZfyzA.png)

![](https://cdn-images-1.medium.com/max/1024/1*eWcRFeoK0ZocwdUliKkrPQ.png)

Testing is essential don’t make it a secondary item for your application development phases. It will give you the confidence that your application is still working every release.

In my present team, we build things using AngularJS, and we don’t have the confidence that we can remove items or change class name even we can’t delete a file that we are sure that this file doesn’t used anywhere. My advice for any new team to set testing as standards for their team.

#### Conclusion

Don’t afraid of using this library, give it a try and read more about it. I think it will be an advantage for testing react application using jest. I heard people all the time afraid of these things and said it would add complexity. However, I am working now with a big team, and they don’t consider stuff for the long-term, this will goes wrong when your application goes significantly, after a while this will reduce the confidence of our front-end project, and this gives a miserable feeling every day.

In the end, It is incredible how people work every day to help other people make their coding life easy. I hope I will be one of them soon. Thank you for everyone give something new for the community.

Check this code I have created it on my codesandbox:

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2F1yzvlxk6j%3Fmodule%3D%252Fsrc%252FApp.test.js%26view%3Deditor&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2F1yzvlxk6j%3Fmodule%3D%252Fsrc%252FApp.test.js%26view%3Deditor&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F1yzvlxk6j%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bfe9b585703dc21e58ca4049d1edbc3/href">https://medium.com/media/7bfe9b585703dc21e58ca4049d1edbc3/href</a></iframe>