๐Ÿงช React Testing Library

Testing React Components

What is React Testing Library?

React Testing Library (RTL) is a library for testing React components. It encourages testing from the user's perspective rather than implementation details.

๐Ÿ’ป Component Test

import { render, screen, fireEvent } from '@testing-library/react';
import Counter from './Counter';

test('renders counter and increments', () => {
  render();
  
  const button = screen.getByText(/increment/i);
  const count = screen.getByText(/count: 0/i);
  
  expect(count).toBeInTheDocument();
  
  fireEvent.click(button);
  
  expect(screen.getByText(/count: 1/i)).toBeInTheDocument();
});

๐ŸŽฏ Key Takeaways