How to start with test cases in React using Jest?
Published on 11 June, 2021
| Last Updated on 27 October, 2023
Published on 11 June, 2021
| Last Updated on 27 October, 2023
It is an open-source JavaScript library created by Facebook and is used for managing the view layer for mobile and web applications. It is possible to present it on the server-side as well as the client-side. React JS helps simplify the process of mobile application development since the code that has been written at the time of website development can be used once again for creating a web app. React JS is ideal for continuing large amounts of information.
This particular framework has been ranked as the most popular JavaScript testing framework by the stateofjs survey. Facebook has created jest, and the main emphasis with this framework happens to be on simplicity. It offers decent cross-browser support and is used on a wide scale with Selenium for automated testing.
An integrated framework is provided by Jest, which does not need any experience when it comes to the configuration. This tool is all set to use, and it would be possible to set it up instantaneously by running the command-
npm install –save-dev jest
Your requirement will figure out whether it will be better to use Jasmine or Jest for your project. In case the project needs debugging the test cases in an IDE that is not supported by Jest, then your best choice would be Jasmine. However, it will be advisable to try out Jest, given that Facebook has been making lots of investments at present on this framework. Moreover, the React developers are also getting a positive experience after using Jest.
Let’s create sample react application by using following commands:
import "@testing-library/jest-dom";
import { render, unmountComponentAtNode } from "react-dom";
import { act } from "react-dom/test-utils";
let container = null;
beforeEach(() => {
// setup a DOM element as a render target
container = document.createElement("div");
document.body.appendChild(container);
});
afterEach(() => {
// cleanup on exiting
unmountComponentAtNode(container);
container.remove();
container = null;
});
act(() => {
// render components
});
// make assertions
// Hello.jsx
import React from "react";
const Hello = (props) => {
var returnStmnt = "";
if (props.name) {
returnStmnt = <h1>Hello, {props.name}!</h1>;
} else {
returnStmnt = <span>Hey, stranger</span>;
}
return <>{returnStmnt}</>;
};
export default Hello;
// hello.test.js
import React from "react";
import { render, unmountComponentAtNode } from "react-dom";
import { act } from "react-dom/test-utils";
import Hello from "./Hello";
let container = null;
beforeEach(() => {
// setup a DOM element as a render target
container = document.createElement("div");
document.body.appendChild(container);
});
afterEach(() => {
// cleanup on exiting
unmountComponentAtNode(container);
container.remove();
container = null;
});
it("renders with or without a name", () => {
act(() => {
render(<Hello />, container);
});
expect(container.textContent).toBe("Hey, stranger");
act(() => {
render(<Hello name="Mark" />, container);
});
expect(container.textContent).toBe("Hello, Mark!");
});
Jatin Panchal is an innovation-driven entrepreneur, and the Founder & Managing Director of Rlogical Techsoft Pvt. Ltd. He believes modern leadership is driven by innovation, adaptability, and the transformative power of Artificial Intelligence. He focuses on helping businesses accelerate digital growth through AI-powered solutions, intelligent automation, Cloud, blockchain, IoT, and scalable enterprise technologies. With a strong strategic vision and future-focused mindset, he is passionate about building technology ecosystems that improve efficiency, drive innovation, and create long-term business value for global clients.
Global insights on technology trends, best practices, and digital transformation strategies.
A proven track record of high-impact deliveries across industries and technologies