byteNative
Testing & Quality

Testing Library

User-centred component tests that check real behaviour.

What is Testing Library?

Testing Library checks components the way real people actually use them, rather than clinging to internal details. You query elements by their role, label or visible text, not by CSS classes or component internals. That produces tests which secure how an interface behaves and rarely break for no reason during refactors. As a pleasant side effect this mindset nudges you toward more accessible components, because accessible UI is also testable UI.

More in the documentation

How we use it

We use Testing Library for everything in the frontend that has behaviour: forms, modals, lists with states. Instead of pinning down implementation details we test what the user sees and triggers, so you can restructure your markup freely without rewriting tests every time. It is the glue between our unit tests and the larger browser tests.

tsx
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { Counter } from "./Counter";

test("increments on click", async () => {
  render(<Counter />);
  await userEvent.click(screen.getByRole("button", { name: /add/i }));
  expect(screen.getByText("1")).toBeInTheDocument();
});
Query by role instead of by CSS class.

Good to know

Stick to the query priority list: getByRole before getByLabelText before getByText, with testId only as a last resort. For anything after a click you need findBy or waitFor, because React updates are async and getBy would otherwise fail too early.

00Testing & Quality

More tools we work with in the same area.

Which technology fits you?

You don't have to decide that, it's our job. Tell us about your plans.