Get a free consultation
ReactJS Development

Unit testing in React (using tools like Jest and Enzyme)

By Sigmato Editorial TeamUpdated 14 Nov 20261 min read

Unit testing is an important part of building robust React applications. Jest is a popular JavaScript testing framework that works well with React, and Enzyme is a testing utility library with additional tools for testing React components. Here is a guide to unit testing in React using Jest and Enzyme.

Setting Up Jest and Enzyme

  1. Install Dependencies:

    • Install Jest, Enzyme, and necessary Jest plugins.

2. Configure Jest:

  • Add a jest.config.js file to your project for Jest configuration.

3. Configure Enzyme:

  • Create a setupTests.js file for configuring Enzyme.

Writing Unit Tests

  1. Basic Jest Test:

    • Write a simple Jest test for a function.

2. Testing React Components with Jest and Enzyme:

  • Test a React component using Jest and Enzyme.

Common Jest Matchers

  • toBe(value): Checks if the received value is strictly equal to the expected value.
  • toEqual(value): Recursively checks if the received and expected values are deeply equal.
  • toContain(value): Checks if an array or string contains the expected value.
  • toHaveBeenCalled(): Checks if a function has been called.
  • toHaveBeenCalledWith(arg1, arg2, ...): Checks if a function has been called with specific arguments.

Running Tests

  • Run All Tests:

Run Tests in Watch Mode:

Run Tests for a Specific File:

Snapshot Testing

  • Snapshot Testing with Jest:

    • Jest lets you create and update snapshots of the rendered component tree.

Update Snapshots:

  • When a component’s structure changes intentionally, you can update the snapshots.

Unit testing helps maintain code quality and catch issues early in development. Jest and Enzyme together make it straightforward to test React components, helping your application behave as expected as it evolves.

Sigmato Editorial Team

Written by

Sigmato Editorial Team

The Sigmato Editorial Team is made up of software engineers, UI/UX designers and AI specialists at Sigmato Solutions Pvt Ltd in Bengaluru, India and the UK. We write practical guides on web, mobile, cloud and AI development, drawing on the products we design and build for startups and enterprises worldwide.

Last updated 14 November 2026 · Talk to our team