React Native theorytheory 0/50 · 0%
Testing · medium

32. Testing components

Unit and component tests with Jest and Testing Library.

Jest is the default test runner for React Native, and `@testing-library/react-native` lets you test components the way a user interacts with them — by text and role, not implementation details.

import { render, screen, fireEvent } from "@testing-library/react-native";

test("increments counter on press", () => {
  render(<Counter />);
  fireEvent.press(screen.getByText("0"));
  expect(screen.getByText("1")).toBeTruthy();
});

Pure logic (formatters, reducers, validators) should be tested directly as plain functions — it's faster and more reliable than rendering a component just to check a calculation.

Check your understanding

  1. 1. What is the default test runner for React Native?

  2. 2. What philosophy does Testing Library encourage?

  3. 3. Where should pure logic like a formatter be tested?