Level-Up Your React Skill with Testing (Part 1)

Are you currently learning React JS or using it in your work ?

Let’s step up the game by learning how to test our React App. We will learn how to test using packages that comes bundled with create-react-app, Jest and React Testing Library. I…


This content originally appeared on DEV Community and was authored by Fadhil Radhian

Are you currently learning React JS or using it in your work ?

Let's step up the game by learning how to test our React App. We will learn how to test using packages that comes bundled with create-react-app, Jest and React Testing Library. In this first part of the series, let's understand about these two packages and a simple test.

Jest

Jest is a testing library developed by Facebook to find, run tests and determine whether the tests pass or fail. It is also the test runner that recommended by React Testing Library.

React Testing Library

React Testing Library is an open-source package, part of Testing Library package. Its job is to provide Virtual DOM for the tests and interact with the Virtual DOM while testing. For convenience, we will refer React Testing Library as RTL throughout this article.

So, basically these two packages will complement each other while testing our React app.

Getting Started with Simple Test

Let's start by using test that comes bundled with create-react-app. As you've probably done before, execute npx create-react-app or yarn create react-app in your terminal, then cd to your folder and run npm test or yarn test in your terminal. You will see the following :

first test

Wow, we pass our first test immediately :) . You may think : "Wait, how does that happen ? And what am I testing ?". Sure, we will analyze how this test works.

Now let's open the package.json file. Under dependencies, you will see :

Create React App dependencies

As I mentioned earlier, you can see that these packages are already installed :
@testing-library/jest-dom = RTL and Jest combined
@testing-library/react = RTL core package
@testing-library/user-event = RTL package for handling user event

Those are packages needed to run the test. Then, let's identify the test files in src folder, that usually get deleted in almost every React tutorial video :)

  1. setupTests.js :
    setupTests.js
    As you can see, here Jest-DOM package is imported. This allows us to use Jest methods that we will further discuss later.

  2. App.test.js
    App.test.js

Here is where the action happens. Let's break down the code step by step.

import { render, screen } from '@testing-library/react';
import App from './App';

In this file, we import render method and screen object from RTL. render method's job basically is to give us Virtual DOM for testing, whereas screen object is where the methods for testing available for us to use to access the Virtual DOM created by render method. There are many methods that we can use to test our app and we will discuss some of them later.

test("renders learn react link", () => {
   // rest of the code
});

The test() method here is a method provide by Jest, which takes two arguments :

  1. A String, for test name. It can be any name you want. I recommend the name to describe what the test is about.
  2. A function, that contains the test. Here is where the magic happens.

Now let's analyze the full code :

test('renders learn react link', () => {
  render(<App />);
  const linkElement = screen.getByText(/learn react/i);
  // rest of the code
});

What happens here is we tell RTL to get 'learn react' text in the Virtual DOM.

The render method does it job by providing the Virtual Dom for App component. We also create linkElement variable. We then assign it to getByText method from screen object that accepts /learn react/i as argument. /learn react/i is a Regex that means learn react string and i means case-insensitive. We also can use just plain 'Learn React' string. We don't have to use Regex.

test('renders learn react link', () => {
  render(<App />);
  const linkElement = screen.getByText(/learn react/i);
  expect(linkElement).toBeInTheDocument();
});

In the final line, what happened is we tell Jest to test whether the linkElement exists in the Virtual DOM or not.

expect is a method provided by Jest to test our app. We have to pass the part that we want to test as an argument. Then we test it with other method. As its name suggest, toBeInTheDocument() method's job is to test whether linkElement exist in the document (Virtual DOM).

Why the test pass ?

Let's open App.js file :

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;

You can see that <a> tag containing Learn React text exist here. The test's job is to make sure that this element shows up correctly in the Virtual DOM. The test pass because it renders correctly, as you can test when running the project and inspect the element :

Learn React link

And that's it! Now you know how this simple test works. But, this is not how test our app for production, it would be more complex and should simulate how our user interacts with the app. We will test with more example in the next part of this series.

Thanks for reading and have a good day!

Sources :
https://jestjs.io/
https://testing-library.com/docs/react-testing-library/intro/
https://reactjs.org/docs/faq-internals.html

Connect with me :
https://www.linkedin.com/in/fadhil-radhian/
Github : fadhilradh


This content originally appeared on DEV Community and was authored by Fadhil Radhian


Print Share Comment Cite Upload Translate Updates
APA

Fadhil Radhian | Sciencx (2022-02-12T04:51:38+00:00) Level-Up Your React Skill with Testing (Part 1). Retrieved from https://www.scien.cx/2022/02/12/level-up-your-react-skill-with-testing-part-1/

MLA
" » Level-Up Your React Skill with Testing (Part 1)." Fadhil Radhian | Sciencx - Saturday February 12, 2022, https://www.scien.cx/2022/02/12/level-up-your-react-skill-with-testing-part-1/
HARVARD
Fadhil Radhian | Sciencx Saturday February 12, 2022 » Level-Up Your React Skill with Testing (Part 1)., viewed ,<https://www.scien.cx/2022/02/12/level-up-your-react-skill-with-testing-part-1/>
VANCOUVER
Fadhil Radhian | Sciencx - » Level-Up Your React Skill with Testing (Part 1). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/12/level-up-your-react-skill-with-testing-part-1/
CHICAGO
" » Level-Up Your React Skill with Testing (Part 1)." Fadhil Radhian | Sciencx - Accessed . https://www.scien.cx/2022/02/12/level-up-your-react-skill-with-testing-part-1/
IEEE
" » Level-Up Your React Skill with Testing (Part 1)." Fadhil Radhian | Sciencx [Online]. Available: https://www.scien.cx/2022/02/12/level-up-your-react-skill-with-testing-part-1/. [Accessed: ]
rf:citation
» Level-Up Your React Skill with Testing (Part 1) | Fadhil Radhian | Sciencx | https://www.scien.cx/2022/02/12/level-up-your-react-skill-with-testing-part-1/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.