Logo

dev-resources.site

for different kinds of informations.

Setup Jest, Babel and testing library for unit testing in React

Published at
3/26/2024
Categories
react
jest
babel
testinglibrary
Author
griseduardo
Categories
4 categories in total
react
open
jest
open
babel
open
testinglibrary
open
Author
11 person written this
griseduardo
open
Setup Jest, Babel and testing library for unit testing in React

Introduction

During code development, writing tests becomes important to bring more security and reliability to the process. Since March 2023, create-react-app ceased to be suggested as an option to start a React application in the official documentation. The purpose of this article is to provide a simple configuration using Jest, Babel and testing-library to enable unit testing in React, without using the included in create-react-app for this purpose.

Babel

It's a JavaScript compiler used to convert ECMAScript 2015+ code to an earlier version of JavaScript compatible with browsers and environments.

Jest

It's a testing framework created by Facebook, with simple configuration and usage, allowing tests to be run in isolation.

Testing library

It's a lightweight library that allows simulating tests in a way similar to how the user will interact with the application.

Setup

To add Babel:

yarn add @babel/core @babel/preset-env @babel/preset-react babel-jest --save-dev

  • @babel/core: brings the base of Babel to application.
  • babel-jest, @babel/preset-react: they enable the transformation of JavaScript code inside the testing environment, to enable test execution.
  • @babel/preset-env: it allows you to use the latest version of JavaScript without needing to define which syntax transformation is necessary to be compatible with the environment to be used.

To add Jest:

yarn add jest jest-environment-jsdom --save-dev

  • jest: enables the execution of unit tests.
  • jest-environment-jsdom: provides the JSDOM testing environment, which simulates a DOM environment as if it were in the browser.

To add testing-library:

yarn add @testing-library/react @testing-library/jest-dom @testing-library/user-event --save-dev

  • @testing-library/react: adds the testing-library for use in React applications.
  • @testing-library/jest-dom: brings additional matchers for Jest tests, making them more declarative.
  • @testing-library/user-event: allows simulating interactions that users will have with the application, such as clicks, typing.

Add Jest configuration file, jest.config.js , to the root of the project:



const config = {
  testEnvironment: "jsdom",
};

module.exports = config;


Enter fullscreen mode Exit fullscreen mode

Where in testEnvironment will be set the test environment.

Add Babel configuration file, babel.config.js , to the root of the project:



module.exports = {
  presets: [
    "@babel/preset-env",
    "@babel/preset-react",
  ],
};


Enter fullscreen mode Exit fullscreen mode

Where in presets will be defined the presets to be used.

Add script to run tests in package.json:



"scripts": {
  "test": "jest",
}


Enter fullscreen mode Exit fullscreen mode

The formats of the test files to be executed by default:

  • .test.js
  • .spec.js
  • .js if inside __tests__ folder

Running yarn test will execute the tests.

Execution example

Now I will present an example of running a test after the configuration has been done, to demonstrate the result.

An example component will be defined in Example.js, which renders the text Example:



import React from "react";

const Example = () => (
  <h1>Example</h1>
)

export default Example;


Enter fullscreen mode Exit fullscreen mode

And a test file for the component Example.test.js, which will validate if the text Example appears after its rendering:



import React from "react";
import "@testing-library/jest-dom";
import { render, screen } from "@testing-library/react";

import Example from "./Example";

describe("<Example />", () => {
  it("should render component", () => {
    render(<Example />);

    const element = screen.getByText("Example");

    expect(element).toBeInTheDocument();
  });
});


Enter fullscreen mode Exit fullscreen mode

Finally, yarn test was executed in the terminal, obtaining the following result:

Image description

Conclusion

The idea is to present a setup using Babel, Jest and testing-library for unit tests in React, without relying on what is included in create-react-app for this purpose. The example above was mainly to demonstrate that the test was successfully executed after the configuration. In the next article, I will focus more on how tests work and the different scenarios that can be tested using testing-library with Jest.

babel Article's
30 articles in total
Favicon
Manual Setup (Custom Webpack/Babel configuration) with react (i am not able running it )
Favicon
Babel and Sourcemaps, Part-1
Favicon
Imagining React Without JSX: A Developer's Worst Nightmare
Favicon
Explorando org-babel en emacs
Favicon
Introduction to custom Babel plugins
Favicon
How React JSX Gets Transformed Into JavaScript Behind the Scenes
Favicon
Why react components starts with capital letter ?
Favicon
Fixing Jest import failure
Favicon
Comprehensive Analysis of Industry-standard Build Tools
Favicon
Making a Logging Plugin with Transpiler
Favicon
Mastering Project Maintainability with Module Resolver
Favicon
Understanding Webpack and Babel: Key Tools for Modern JavaScript Development
Favicon
How I optimized Carousel for EditorJS 2x in size.
Favicon
Let's Talk About Babel: Have you ever stopped to understand it?
Favicon
Setup Jest, Babel e testing library para testes unitรกrios em React
Favicon
Setup Jest, Babel and testing library for unit testing in React
Favicon
Exploring Marvels of Webpack - Ep 1 - React Project without CRA
Favicon
What is Babel.js? Uses, Advantages, and Disadvantages
Favicon
The Comprehensive Guide to Babel
Favicon
Why to use babel ?
Favicon
Configuring StyleX in React application
Favicon
React Development Essentials: Webpack and Babel Introduction
Favicon
Abstract Syntax Trees and Practical Applications in JavaScript
Favicon
Create React App From Scratch With Isomorphic Rendering
Favicon
How React uses pragmas to run tests conditionally
Favicon
Unveiling the Artistry Behind JavaScript's Babel Engine: Transforming Code Across Frontiers
Favicon
Express & ES6 Boilerplate
Favicon
An introduction to Babel
Favicon
Building Cross-Browser Compatible Web Apps with Babel: A Step-by-Step Guide
Favicon
Configure Stimulus with esbuild and Babelโ€Šโ€”โ€ŠRails & Javascript

Featured ones: