Skip to content
EcosystemChapter 37 / 37Advanced5 min read

Testing React Apps

React testing library with Vitest for beginners. Set up Vitest, use render and screen queries, and test components like a user with user-event.

On this page

1. What is Testing React Apps?

The common stack today is: Vitest (the test runner, fast with Vite), React Testing Library (rendering components and finding UI), and user-event (real user-like clicks and typing).

The philosophy of React Testing Library is simple: do not test a component's internal state or functions, test only what the user sees and does.

2. Why does it matter?

Opening every page by hand after every change is slow, and things get missed. Tests do this work in seconds, the same way every time.

Manual testing onlyWith automated tests
Clicking around the browser after every changenpm test checks everything
Old features quietly breakA regression is caught immediately
Refactoring feels scaryGreen tests give confidence
The bug is found in productionThe bug is stopped in CI

3. Where is it used?

Test typeWhat it checksTool
UnitOne function or hookVitest
ComponentOne component's render and interactionVitest + Testing Library
IntegrationSeveral components together, like a form + listVitest + Testing Library
End to endThe whole app in a real browserPlaywright

4. How does it work?

Vitest runs in Node. The jsdom environment provides a fake browser DOM. Testing Library renders the component into that DOM, and you find elements with screen.

The flow of one test
render(component)
Find an element with screen.getByRole
Click or type with user-event
Check the result with expect
The test passes or fails

Remember the query priority: getByRole first, then getByLabelText, then getByText. getByTestId is the last resort.

5. Code example

Setup

bash
npm install -D vitest jsdom @testing-library/react @testing-library/dom @testing-library/user-event @testing-library/jest-dom
ts
import { defineConfig } from "vitest/config";
import react from "@vitejs/plugin-react";

export default defineConfig({
  plugins: [react()],
  test: {
    environment: "jsdom",
    globals: true,
    setupFiles: "./vitest.setup.ts",
  },
});
ts
import "@testing-library/jest-dom/vitest";

The first file is vitest.config.ts and the second is vitest.setup.ts, which adds matchers like toBeInTheDocument.

Example 1: A counter test

tsx
import { useState } from "react";

export function Counter() {
  const [count, setCount] = useState(0);
  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
tsx
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it } from "vitest";
import { Counter } from "./Counter";

describe("Counter", () => {
  it("increases the count on click", async () => {
    const user = userEvent.setup();
    render(<Counter />);

    const button = screen.getByRole("button", { name: /count: 0/i });
    await user.click(button);

    expect(screen.getByRole("button", { name: /count: 1/i })).toBeInTheDocument();
  });
});

Example 2: Testing async UI

tsx
it("shows the name when the data arrives", async () => {
  render(<UserCard userId={1} />);

  expect(screen.getByText(/loading/i)).toBeInTheDocument();
  expect(await screen.findByText("Ali")).toBeInTheDocument();
});

How does the code work?

PartMeaning
environment: "jsdom"A browser-like DOM inside Node
globals: trueLets Testing Library clean up on its own after every test
userEvent.setup()A user instance at the start of each test
await user.click(...)user-event actions are async
getByRole("button", ...)Finds by accessible role and name
findByTextWaits until the element appears (async)
toBeInTheDocument()A jest-dom matcher

Interview tip

You will almost certainly be asked the difference between getBy, queryBy and findBy. getBy throws an error if it finds nothing, queryBy returns null (for testing that something is not there), and findBy returns a Promise that waits until the element appears.

6. Common mistakes

Mistake: Testing implementation details

Why? The test breaks when you rename state or an internal function, even though nothing changed for the user.

Better: Test only what the user sees: text, roles, labels and their behavior.

Mistake: Not awaiting user-event actions

Why? The expect runs before the action finishes and the test fails at random.

Better: Call userEvent.setup() and put await before every user.click or user.type.

Mistake: Using getBy for async UI

Why? The data has not arrived yet, so getBy fails immediately.

Better: Use await findBy for elements that will appear later.

Mistake: Adding data-testid to everything

Why? The tests do not check accessibility and drift away from the user's point of view.

Better: Try getByRole and getByLabelText first, and keep testid as the last option.

7. What to learn before and after

Final quick revision

PointDetail
RunnerVitest, environment: "jsdom"
Renderrender() and screen
QueriesgetByRole first, findBy for async
InteractionuserEvent.setup() and await user.click()
Matchers@testing-library/jest-dom/vitest