Testing React Components Explained for Beginners | by Gerard van der Put | May, 2022

Testing code might be intimidating when you don’t know the way it works. We’ll undergo the fundamentals

Photograph by Digital Buggu on Pexels.


Meet James. James is an element of a big growth workforce. He focuses on creating superior React elements, and along with many colleagues, he’s constructing a big frontend.

Proper now, he’s constructing a model new button.

This button has one necessary purposeful requirement: After a consumer clicks on it, it must turn into disabled. It’s no drawback creating this, and James pushes his code into the repository in order that it turns into accessible for all different builders once they pull within the newest modifications.

However wait. That’s fairly scary, isn’t it? What if one other developer from the workforce modifications the code? What if the button not turns into disabled after a consumer clicks on it?

That is the place testing comes into play.

James can add a take a look at for his new button element that asserts that the button turns into disabled after clicking on it (we’ll describe how a take a look at truly does this a bit additional down under). And every time somebody modifications the code for the button element, and (by accident or not) modifications it in a means in order that the button not is disabled after a consumer clicks on it, the take a look at will fail.

With huge pink alerts. Methods and pipelines will cease, break, scream and cry. Emails are despatched mechanically, stating that the button not turns into disabled after a consumer clicks on it!

This can be a little bit of a infantile instance in fact. However hopefully, it explains the idea of testing. As a matter of reality, that is precisely how giant tasks be certain that nothing “breaks.”

See for your self: Examples of assessments might be discovered over at React, lodash, date-fns, and just about each different venture that respects itself. That is what number of builders can work collectively on one venture and ensure they don’t break one another’s code and performance.

Let’s dive a bit extra into testing!

We’ll clarify all the main points alongside the best way. The summary instance above was defined in a easy matter, however a couple of questions are to be answered.

Very fundamental take a look at

Earlier than we have a look at how we are able to run assessments, let’s take a look at a really minimal code instance:

The button element is a quite simple React element. It provides an HTML button aspect to the DOM when it’s rendered.

However let’s take a look on the take a look at itself. First, we import a renderperform from the React testing-library and the button element (line 7-8).

Then we name a perform known as it (which is an alias for a perform known as “take a look at,” which is a global function from Jest). We describe what we’re testing (the title for this little take a look at), and we go in an arrow perform containing the take a look at’s logic.

The logic might be discovered at strains 11–13. On line 11, we render our Button element. Discover that the render perform returns — amongst different issues — a container. The container is an HTML aspect containing the weather which are returned by the render perform. It’s the basis of the doc during which we execute our assessments (see the notice about this container under).

After the button is rendered contained in the container (line 11), we question the container for a button aspect (line 12). And at last, on line 13, we make our first assertion:


Studying this line speaks for itself because of the readable syntax of Jest. In our take a look at, we anticipate that the worth for the variable button is just not null. And when the take a look at runs (extra about easy methods to run assessments quickly), it succeeds, and everyone seems to be pleased!

Notice: usually, you don’t need to work with the render perform’s container reference. We’ll have a look at a number of helper capabilities quickly which are extra handy to work with. The default ESLint configuration in CRA (Create React App) is even complaining when your work with native node-related capabilities similar to querySelector. However we needed for example a really minimal instance that almost all builders will perceive at first look.

However now, what if a junior developer modified the code for our button element to the next? Let’s say they changed the HTML button aspect with a div aspect contained in the button element (line 2):

When the take a look at runs once more, now it is going to fail. As a result of it doesn’t discover an HTML button aspect, the worth for the button variable will likely be null:

Hopefully, the junior developer now begins a dialogue with the creator of the failing take a look at, and collectively, they’ll discover out what to do. They both have to alter the take a look at or agree they won’t change the button aspect right into a div aspect.

If this take a look at didn’t exist, no person would discover the button aspect was become a div. And once more, that is an oversimplified instance. However hopefully, you slowly begin to perceive how good assessments for our elements and code could make our lives higher, particularly once we’re collaborating with different individuals.

Notice: assessments will not be solely helpful once you work along with different individuals. Even when you could have a personal venture only for your self, writing good assessments (at the least for essential elements of your code) can forestall you from getting some complications sooner or later, when you could have forgotten why you wrote sure items of logic in the best way you probably did. Checks are guarding your code, let’s imagine. With out them, you’ll be able to simply introduce bugs with out being conscious of it!


Earlier than we have a look at how we are able to run assessments, it’s value mentioning that we’ll usually work with parts within the DOM once we write our assessments. The testing-library venture comprises an important package deal known as jest-dom, which gives a set of helper capabilities for making assertions associated to the DOM.

You’ll be able to view all matchers (as they’re known as) within the jest-dom readme file.

Jest-dom is included by default once you create a brand new utility with CRA (Create React App).

Operating assessments with CRA (Create React App)

Create a brand new utility with CRA:

$ npx create-react-app MyTestApplication

When the set up is finished, go into the listing and run the assessments for this venture:

$ cd MyTestApplication
$ yarn run take a look at --watchAll

Notice: how we use the watchAll flag to make sure that all assessments are executed. If we don’t present this flag, solely assessments that you simply make modifications to will run.

There may be one take a look at (src/App.take a look at.js), and it succeeded! Let’s have a look at the content material of that file:

After every part we mentioned above, this take a look at ought to make sense by now. Apart from one factor.

We don’t work with the container reference anymore. As an alternative, we are able to make the most of the display screen reference that’s imported on line 1. This display screen occasion has a number of helper capabilities (similar to getByText ) that we are able to use to search out parts on our digital “display screen” (e.g., the DOM). On this case, we question for any HTML aspect that comprises the textual content “study react.”

And at last, we anticipate that aspect to be within the doc (DOM).

That is how we run assessments manually in any CRA venture. It’s a pleasant means of getting your palms soiled for the primary time since you don’t have to put in and configure Jest.

You’ll be able to learn extra about putting in and configuring Jest over here when you’re . It’s not that difficult.

One very last thing is necessary to say. The way in which Jest is configured for CRA tasks is that it’ll execute assessments for:

  • Recordsdata with .js suffix in __tests__ directories
  • Recordsdata with .take a look at.js or .spec.js suffix

You’ll be able to learn extra concerning the setup over here. And it may be custom-made, in fact.

Including assessments to your repositories can defend your code, so to talk. It could actually assert and test that your code is doing what you anticipate it to do. And if it doesn’t, the assessments will fail.

On this article, we executed assessments manually. However they will (and may) be added to your pipelines you probably have a continuous integration setup to be able to forestall code from being deployed if any of the assessments fail.

I bear in mind being extraordinarily sceptical about testing once I first realized about it, many moons in the past. However slowly however certainly I found out how highly effective it was. And the way good it’s to have them “watching over your code.”

We solely scratched the floor on this article, so maybe I’ll dive into some extra superior matters quickly.

Thanks on your time!


More Posts