Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Using React Testing Library (RTL)
3.
Rendering Components in React Testing Library(RTL)
4.
Selecting elements in React Testing Library(RTL)
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

RTL Introduction and Features

Author Parth Jain
0 upvote

Introduction

RTL, or the React Testing Library developed by Kent C. Dodds, was released as an alternative to Enzyme.js. As discussed in our blogs on Enzyme, we know that Enzyme provides React developers utilities to test the internals of a React component. React Testing Library, in particular, questions "how to test a React component in order to get full confidence in our React components." Rather than testing a component's implementation details like in Enzyme, React Testing Library allows a developer to be in the shoes of an end-user of a React-based application.
This blog will help you understand the React Testing Library(RTL) and its various features.

Using React Testing Library (RTL)

When a  react application is created using the Create React App method, It already includes both the React Testing Library(RTL) and Jest as default. Hence, we can directly get started writing test codes.
In case we want to use React Testing Library without creating a CRA application, then all we need to do is install both React Testing Library and Jest using NPM.
npm install --save-dev @testing-library/react jest
We installed Jest as React Testing Library(RTL) only provides methods that help writing test scripts. Hence, we still require a Javascript testing framework like Jest or Mocha to run the test code.

Rendering Components in React Testing Library(RTL)

It is time that we learn how a react component is rendered using RTL for your tests. In the codes below, we implement RTL on the following App component.

import React from 'react';
const heading = 'Hello Ninja!';
function App() {
    return <div>{heading}</div>;
}
export default App;

Now we test it in the App.test.js file

import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';
describe('App', () => {
    test('renders the App component', () => {
    render(<App />);
    screen.debug();
  });
});

Finally, we run the test using the npm test command. This command will allow us to see the HTML output of the test application.
The screen.debug() method used in the above code is used as a shortcut for console.log(prettyDOM()) as it supports debugging the document, a single element, or an array of elements. 
Whenever we write a test for a component in the React Testing library(RTL), we can render the component first and then debug what is visible for RTL’s renderer in the test. This allows us to write tests more confidently.

Output:

<body>
  <div>
    <div>
      Hello Ninja!
    </div>
  </div>
</body>

Selecting elements in React Testing Library(RTL)

After rendering our React component, RTL allows us various search functions to grab elements. These elements can later be used for assertions or for user interactions.

import React from 'react';
import { render, screen } from '@testing-library/react';
import App from './App';
describe('App', () => {
    test('renders the App component', () => {
    render(<App />);
    screen.getByText('Search:');
  });
});

After using the screen.debug() function. It is clear to observe what all elements are present in the final output. When we are well versed with the HTML structure of our application, we can select elements using RTL's screen object function easily. The selected elements can later be used for user interactions or assertions. 
The getByText function accepts a string as an argument.

Similar to getByText function the React testing library(RTL) provides the following functions to select an element in the final HTML.

  • getByText
  • getByRole
  • getByLabelText
  • getByPlaceholderText
  • getByAltText
  • getByDisplayValue
  • queryBy variants:
  • queryByText
  • queryByRole
  • queryByLabelText
  • queryByPlaceholderText
  • queryByAltText
  • queryByDisplayValue

To Find elements by variants.

  • findByText
  • findByRole
  • findByLabelText
  • findByPlaceholderText
  • findByAltText
  • findByDisplayValue

FAQs

  1. Does React testing library use Jest?
    The React Testing Library(RTL) only provides methods that help writing test scripts. Hence, we still require a JavaScript testing framework like Jest to run the test code.
  2. Is the Jest and React testing library the same?
    Jest provides a great iteration speed and powerful features such as mocking modules and timers that offer more control over how a code executes. React Testing Library, however, is a set of helpers that lets us test React components without relying on their implementation details.
  3. Who created React testing library?
    The React Testing Library(RTL) was developed by Kent C. Dodds, a React educator, and an open-source developer. 
  4. How does React testing library(RTL) render?
    By default, React Testing Library creates a div and appends that div to the body of a document. This is where the React component will be rendered.
  5. What is the React testing library?
    React Testing Library(RTL) is a testing utility tool that is used to test the actual DOM tree rendered by React in the browser. This library aims to help write tests that resemble how an end-user would use an application.

Key Takeaways

In this article, we have extensively discussed the React Testing Library(RTL) and its various features.If you are Preparing for interview and don't know where to start, we have got you covered, check out our expert curated courses on our website, You can also check out Coding Ninjas Studio to practice frequently asked interview problems. We hope that this blog has helped you enhance your knowledge regarding Debugging tests in React and if you would like to learn more, check out our articles. Do upvote our blog to help other ninjas grow. Happy Coding !!

Live masterclass