codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚛️

React.js

44 / 61 topics
43Introduction to React Testing44Unit Testing with Jest45Snapshot Testing in React46End-to-End Testing with Cypress47Using React Developer Tools for Debugging
Tutorials/React.js/Unit Testing with Jest
⚛️React.js

Unit Testing with Jest

Updated 2026-04-20
3 min read

Unit Testing with Jest

Unit testing is a crucial part of software development, ensuring that individual components work as expected. In this section, we will dive into unit testing with Jest, a popular JavaScript testing framework, specifically tailored for React applications.

Introduction to Jest

Jest is a delightful JavaScript Testing Framework with a focus on simplicity and speed. It works seamlessly with React and provides powerful features such as snapshot testing, mocking, and coverage reports. Jest is known for its easy setup and intuitive API, making it an excellent choice for React developers.

Setting Up Jest in a React Project

To get started with Jest in your React project, you need to install Jest along with the react-test-renderer package, which helps in rendering React components into static HTML. You can set up Jest using npm or yarn:

npm install --save-dev jest react-test-renderer @testing-library/react @testing-library/jest-dom

or

yarn add --dev jest react-test-renderer @testing-library/react @testing-library/jest-dom

After installation, you need to configure Jest if your project structure is not standard. You can create a jest.config.js file in the root of your project:

module.exports = {
  setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],
};

Writing Your First Test

Let's write a simple unit test for a React component. Assume we have a Button component that takes a label and an onClick handler as props.

// Button.js
import React from 'react';

const Button = ({ label, onClick }) => {
  return <button onClick={onClick}>{label}</button>;
};

export default Button;

To test this component, we will create a Button.test.js file:

// Button.test.js
import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Button from './Button';

test('renders button with correct label', () => {
  const { getByText } = render(<Button label="Click Me" />);
  expect(getByText('Click Me')).toBeInTheDocument();
});

test('calls onClick handler when clicked', () => {
  const handleClick = jest.fn();
  const { getByText } = render(<Button label="Click Me" onClick={handleClick} />);
  
  fireEvent.click(getByText('Click Me'));
  expect(handleClick).toHaveBeenCalledTimes(1);
});

Explanation

  • render: This function renders the component into a virtual DOM.
  • fireEvent: This utility simulates user interactions like clicks, inputs, etc.
  • jest.fn(): Creates a mock function that can be used to track calls and arguments.

Mocking Dependencies

In real-world applications, components often depend on external libraries or APIs. Jest provides powerful mocking capabilities to isolate the component under test.

Example: Mocking an API Call

Assume we have a DataFetcher component that fetches data from an API:

// DataFetcher.js
import React, { useEffect, useState } from 'react';

const DataFetcher = () => {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch('https://api.example.com/data')
      .then(response => response.json())
      .then(data => setData(data));
  }, []);

  return <div>{data ? data.message : 'Loading...'}</div>;
};

export default DataFetcher;

To test this component, we need to mock the fetch function:

// DataFetcher.test.js
import React from 'react';
import { render, screen } from '@testing-library/react';
import DataFetcher from './DataFetcher';

jest.mock('node-fetch');

test('renders loading state initially', () => {
  render(<DataFetcher />);
  expect(screen.getByText('Loading...')).toBeInTheDocument();
});

test('fetches data and displays message', async () => {
  const mockResponse = { json: jest.fn().mockResolvedValue({ message: 'Hello World' }) };
  global.fetch.mockResolvedValue(mockResponse);

  render(<DataFetcher />);
  await screen.findByText('Hello World');
});

Explanation

  • jest.mock('node-fetch'): Mocks the fetch function globally.
  • global.fetch.mockResolvedValue: Sets up a mock response for the fetch call.

Snapshot Testing

Snapshot testing is a feature provided by Jest that allows you to capture the rendered output of a component and compare it against a saved snapshot. This is useful for catching unintended changes in the UI.

Example: Snapshot Test for Button Component

// Button.test.js
import React from 'react';
import { render } from '@testing-library/react';
import Button from './Button';

test('matches button snapshot', () => {
  const { asFragment } = render(<Button label="Click Me" />);
  expect(asFragment()).toMatchSnapshot();
});

Explanation

  • asFragment(): Returns the rendered component as a document fragment.
  • toMatchSnapshot(): Compares the current output with the saved snapshot.

Best Practices for Unit Testing with Jest

  1. Test One Thing at a Time: Each test should focus on a single aspect of the component's functionality.
  2. Use Descriptive Test Names: Clear and descriptive names help in understanding what each test is verifying.
  3. Mock External Dependencies: Isolate your tests by mocking external libraries or APIs.
  4. Keep Tests Independent: Ensure that tests do not depend on the order of execution.
  5. Regularly Update Snapshots: When intentional changes are made to the UI, update snapshots accordingly.

Conclusion

Unit testing is essential for maintaining a robust and reliable codebase. Jest provides a comprehensive set of tools and features that make it an excellent choice for React applications. By following best practices and leveraging Jest's powerful capabilities, you can ensure that your components work as expected and are resilient to changes.

In the next section, we will explore integration testing with Jest, which involves testing multiple components working together. Stay tuned!


PreviousIntroduction to React TestingNext Snapshot Testing in React

Recommended Gear

Introduction to React TestingSnapshot Testing in React