Tillitsdone
down Scroll to discover

Testing React Components with Jest and RTL

Learn how to effectively test React components using Jest and React Testing Library.

This guide covers setup, best practices, and advanced testing scenarios with practical examples.
thumbnail

Testing React Components with Jest and React Testing Library: A Comprehensive Guide

Abstract digital landscape with flowing data streams and interconnected nodes featuring bright neon blue and electric yellow streams against a deep space background high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Introduction

Testing is a crucial part of developing robust React applications. As our applications grow in complexity, having a solid testing strategy becomes increasingly important. In this guide, we’ll dive deep into testing React components using Jest and React Testing Library, exploring best practices and real-world examples.

Why Jest and React Testing Library?

Jest and React Testing Library have become the go-to testing tools in the React ecosystem. Jest serves as our test runner and assertion library, while React Testing Library enables us to test components in a way that resembles how users actually interact with our application.

Geometric abstract composition with interconnected hexagons in bright sky blue and golden yellow floating in a crystalline space high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Setting Up Your Testing Environment

First, let’s set up our testing environment. If you’re using Create React App, Jest and React Testing Library are included by default. For other setups, you’ll need to install the following packages:

Terminal window
npm install --save-dev @testing-library/react @testing-library/jest-dom jest

Writing Your First Test

Let’s start with a simple example. Consider this basic Button component:

Button.jsx
const Button = ({ onClick, children }) => {
return (
<button onClick={onClick}>
{children}
</button>
);
};

Here’s how we can test it:

Button.test.jsx
import { render, screen, fireEvent } from '@testing-library/react';
import Button from './Button';
describe('Button Component', () => {
test('renders button with correct text', () => {
render(<Button>Click me</Button>);
expect(screen.getByText('Click me')).toBeInTheDocument();
});
test('calls onClick handler when clicked', () => {
const handleClick = jest.fn();
render(<Button onClick={handleClick}>Click me</Button>);
fireEvent.click(screen.getByText('Click me'));
expect(handleClick).toHaveBeenCalledTimes(1);
});
});

Testing Best Practices

1. Test Behavior, Not Implementation

Focus on testing how users interact with your component rather than testing implementation details:

// ❌ Don't test implementation details
test('should set internal state', () => {
const { result } = renderHook(() => useState(false));
expect(result.current[0]).toBe(false);
});
// ✅ Do test user behavior
test('should show success message when form is submitted', () => {
render(<ContactForm />);
fireEvent.click(screen.getByText('Submit'));
expect(screen.getByText('Message sent!')).toBeInTheDocument();
});

Dynamic flowing waves of energy in bright cyan and neon yellow creating an abstract pattern against a deep blue cosmic background high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

2. Use Proper Queries

React Testing Library provides several queries to find elements. Use them in this order of preference:

  1. getByRole
  2. getByLabelText
  3. getByPlaceholderText
  4. getByText
  5. getByTestId
// Example of proper query usage
test('form submission', () => {
render(<LoginForm />);
// Using getByRole and getByLabelText
const usernameInput = screen.getByLabelText('Username');
const submitButton = screen.getByRole('button', { name: /submit/i });
fireEvent.change(usernameInput, { target: { value: 'testuser' } });
fireEvent.click(submitButton);
});

3. Async Testing

When testing asynchronous operations, use waitFor or findBy queries:

test('loads and displays user data', async () => {
render(<UserProfile userId="123" />);
// Wait for loading to complete
await screen.findByText('User Profile');
expect(screen.getByText('John Doe')).toBeInTheDocument();
});

Advanced Testing Scenarios

Testing Custom Hooks

Use renderHook from @testing-library/react-hooks:

import { renderHook, act } from '@testing-library/react-hooks';
import useCounter from './useCounter';
test('should increment counter', () => {
const { result } = renderHook(() => useCounter());
act(() => {
result.current.increment();
});
expect(result.current.count).toBe(1);
});

Testing Context

When testing components that use Context, wrap them in the provider:

const customRender = (ui, { providerProps, ...renderOptions }) => {
return render(
<ThemeContext.Provider {...providerProps}>{ui}</ThemeContext.Provider>,
renderOptions
);
};

Conclusion

Testing React components effectively requires a good understanding of both Jest and React Testing Library. Remember to focus on testing behavior rather than implementation details, use the right queries, and structure your tests in a way that makes them easy to maintain.

Serene cosmic landscape with floating crystal formations in bright sky blue and sunny yellow creating a harmonious pattern in space high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.