Testing Library
Testing Library is a family of packages (@testing-library/dom, /react, /vue) that query the DOM the way a user does: by role, label, and text, not by component internals.
Detailed Explanation
A component test that reads wrapper.state() or a CSS module hash breaks when you refactor. Testing Library pushes you to render into a real (or jsdom) DOM and to find elements with getByRole("button", { name: "Save" }), getByLabelText("Email"), getByText("Error"). If you cannot find it that way, the UI may be inaccessible.
The query variants matter. getBy* throws when missing (assertions). queryBy* returns null (assert absence). findBy* returns a promise and waits (async UI). getAllBy* / queryAllBy* / findAllBy* are the list forms. Prefer role over testid. data-testid is an escape hatch for icon-only controls you cannot label yet.
userEvent (@testing-library/user-event) fires the event sequences a keyboard and pointer produce, not a single click() with no focus. Use it for forms. fireEvent is lower level.
waitFor retries an assertion until it passes or times out — fetch-driven renders, debounce. Pair with findBy* which is implemented as wait + getBy. Always cleanup (React Testing Library does this in afterEach if you import @testing-library/react).
What it costs you: jsdom limits (no real layout, limited media), and tests that are slower than a pure function test. That is the trade for confidence. Snapshotting the entire DOM is not the Testing Library style; assert the thing the user would notice.
Reach for Testing Library for component behaviour. Reach for Playwright for full-page flows. Reach for a function test when no DOM is involved.
within(section) scopes queries when the page has two "Save" buttons. getByRole with { hidden: true } is how you assert a closed menu still exists in the accessibility tree, or that it does not. Prefer asserting visible behaviour the user can name.
Testing Library's configure({ asyncUtilTimeout }) belongs in setup, not in a single slow test that papers over an extra network hop you should have mocked.
Code Examples
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
function Counter() {
const [n, setN] = React.useState(0);
return (
<button type="button" onClick={() => setN(n + 1)}>
count {n}
</button>
);
}
test("increments", async () => {
const user = userEvent.setup();
render(<Counter />);
await user.click(screen.getByRole("button", { name: /count 0/i }));
expect(screen.getByRole("button", { name: /count 1/i })).toBeInTheDocument();
});Presence vs absence
// Presence vs absence
expect(screen.getByText("Saved")).toBeInTheDocument();
expect(screen.queryByText("Error")).toBeNull();
await screen.findByText("Loaded");Common Gotchas
`getBy` vs `queryBy` vs `findBy`
using `getBy` for something that appears after fetch throws immediately. Use `findBy` or `waitFor`.
`container.querySelector` skips the point of the library
prefer roles and labels so a11y regressions fail the test.
Icon buttons need an accessible name
`aria-label` or visible text, not only a test id.
`toBeInTheDocument` needs `@testing-library/jest-dom` (or the Vitest equivalent
`toBeInTheDocument` needs `@testing-library/jest-dom` (or the Vitest equivalent setup)