Chapter 21:Testing in React

Testing in React — this is one of the most important chapters for writing reliable, production-ready code!

Testing is what separates hobby projects from professional applications. When you test your components and hooks properly, you:

  • Catch bugs early
  • Feel confident refactoring
  • Make sure UI behaves correctly
  • Sleep peacefully when deploying

We’ll focus on the modern, recommended way in 2026:

  • React Testing Library (RTL) – the best tool for testing React components
  • Jest – the test runner (comes with Create React App / Vite by default)

We’ll go very slowly and clearly, like I’m sitting next to you in Mumbai showing you live on the screen, with complete examples you can copy-paste right now.

1. Why React Testing Library (not Enzyme)?

Tool Philosophy Recommendation in 2026
React Testing Library Test how users interact with your app, not implementation details Official recommendation – use this!
Enzyme Tests internal component state & structure Deprecated – don’t use for new code

RTL mantra (repeat after me!):

“The more your tests resemble the way your software is used, the more confidence they can give you.” — Kent C. Dodds

2. Setup (Vite + React + TypeScript)

If you used Vite + React, you already have Jest + Vitest support, but we’ll use Jest for this tutorial.

Install dependencies:

Bash

Add to package.json:

JSON

Create jest.config.js (or jest.config.ts):

TypeScript

Create src/setupTests.ts:

TypeScript

Now you’re ready!

3. Writing Your First Test – Simple Counter Component

Create src/components/Counter.tsx

tsx

Create test file: src/components/Counter.test.tsx

tsx

Run tests:

Bash

4. Testing Hooks with @testing-library/react-hooks (or React 18+ built-in)

Modern way (React 18+): Use renderHook from @testing-library/react

Install:

Bash

Example hook: src/hooks/useCounter.ts

TypeScript

Test file: src/hooks/useCounter.test.ts

TypeScript

5. Best Practices & Common Test Patterns

a. Query Methods (Priority Order)

Method Use when…
getByRole Most preferred – closest to how users find elements
getByLabelText For form inputs
getByPlaceholderText When no label exists
getByText For non-interactive text
getByTestId Last resort – only when nothing else works

b. Async Testing (Data Fetching)

tsx

c. Snapshot Testing (Optional)

tsx

Summary – Chapter 21 Key Takeaways

  • Use React Testing Library – test behavior, not implementation
  • Jest = test runner (easy setup with Vite/Next.js)
  • render, screen, fireEvent, waitFor, findBy* → your main tools
  • Test components → user interactions (click, type, etc.)
  • Test hooks → renderHook + act
  • Priority queries: getByRole > getByLabelText > getByText > getByTestId
  • Write tests that resemble real usage

Mini Homework

  1. Create a simple TodoForm component with input + submit button
  2. Write tests:
    • Renders correctly
    • Typing updates input value
    • Submit calls onSubmit with correct data
  3. Bonus: Test a custom hook like useFetch or useToggle

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *