JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionstesting
PrevNext

Learn the concept

React Testing Library

testing
mid
react-testing-library

How do you test React components with React Testing Library?

react-testing-library
component-testing
userEvent
queries
Quick Answer

React Testing Library tests components from the user's perspective. Use render() to mount components, query methods (getByRole, getByText, etc.) to find elements, fireEvent or userEvent for interactions, and waitFor for async updates.

Detailed Explanation

Philosophy:

  • Test behavior, not implementation
  • Query elements like users would find them
  • Avoid testing internal state/props

Query Methods:

  • getBy*: Throws if not found (sync)
  • queryBy*: Returns null if not found
  • findBy*: Returns Promise (async)

Query Priority:

  1. getByRole: Accessibility roles
  2. getByLabelText: Form elements
  3. getByPlaceholderText
  4. getByText: Non-form elements
  5. getByTestId: Last resort

User Events:

  • userEvent: More realistic than fireEvent
  • Simulates full interaction sequence

Code Examples

Basic component testingJSX
// Counter.jsx
function Counter({ initialCount = 0 }) {
  const [count, setCount] = useState(initialCount);
  
  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(c => c + 1)}>Increment</button>
      <button onClick={() => setCount(c => c - 1)}>Decrement</button>
    </div>
  );
}

// Counter.test.jsx
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import Counter from './Counter';

describe('Counter', () => {
  test('renders initial count', () => {
    render(<Counter initialCount={5} />);
    
    expect(screen.getByText('Count: 5')).toBeInTheDocument();
  });

  test('increments count when button clicked', async () => {
    const user = userEvent.setup();
    render(<Counter />);
    
    await user.click(screen.getByRole('button', { name: /increment/i }));
    
    expect(screen.getByText('Count: 1')).toBeInTheDocument();
  });

  test('decrements count when button clicked', async () => {
    const user = userEvent.setup();
    render(<Counter initialCount={5} />);
    
    await user.click(screen.getByRole('button', { name: /decrement/i }));
    
    expect(screen.getByText('Count: 4')).toBeInTheDocument();
  });
});

Real-World Applications

Use Cases

Testing a complex user registration form with multiple input fields, validation, and submission logic

Using RTL to simulate user typing (`userEvent.type`), clicking buttons (`userEvent.click`), and asserting on displayed error messages (`getByRole('alert')`) or successful submission feedback, ensuring the form behaves correctly from a user's perspective.

Verifying accessibility compliance of UI components (e.g., navigation menu, modal dialog)

Employing `getByRole` and other semantic queries to locate elements, which naturally encourages writing tests that align with accessibility best practices, ensuring components are usable for all users.

Testing a data display component that fetches data asynchronously and shows different states (loading, empty, data loaded)

Using RTL's `findBy*` queries or `waitFor` utility to handle asynchronous updates, asserting that loading indicators appear and disappear correctly, and that data is rendered once available.

Mini Projects

Interactive Counter Component Test

beginner

Build a React counter component with increment/decrement buttons and a display. Write tests using RTL to ensure the count updates correctly on button clicks and displays the initial value.

Login Form with Validation Test

intermediate

Develop a simple login form with email and password fields. Use RTL to simulate user input, trigger validation, and assert on error messages and successful form submission.

Industry Examples

Stripe (React UI)

Stripe uses React for many of its user-facing dashboards and integration elements. React Testing Library is a primary tool for ensuring the robustness and user-centric behavior of these UI components, verifying interactions and accessibility.

Atlassian (Jira/Confluence)

For their enterprise-grade applications like Jira and Confluence, Atlassian leverages React Testing Library to write reliable and maintainable tests for their extensive suite of React components, focusing on how users interact with their complex UIs.

Resources

React Testing Library Docs

docs

Testing Library Cheatsheet

docs

Related Questions

How do you mock functions, modules, and API calls in Jest?

mid
mocking

What is the difference between unit tests and integration tests?

mid
integration
Previous
How do you mock functions, modules, and API calls in Jest?
Next
What is the difference between unit tests and integration tests?
PrevNext