Skip to content

Lesson 13 of 14 · 18m

Testing Components Like a User

Testing Library's one rule, getByRole doubling as an a11y check, msw at the network boundary, and what never earns a test.

The test suite that asserts on useState count, component internals, or a snapshot of a 200-node tree is not a safety net — it's a second implementation of your UI that breaks every refactor. Kent C. Dodds built Testing Library on one sentence: the more your tests resemble the way your software is used, the more confidence they can give you. Everything good in React testing follows from that; everything flaky follows from violating it.

Query like a user, assert like a user

render(<LoginForm />);
await user.type(screen.getByLabelText(/email/i), "a@b.co");
await user.click(screen.getByRole("button", { name: /sign in/i }));
expect(screen.getByText(/check your email/i)).toBeInTheDocument();

getByRole with an accessible name is doing two jobs at once — it finds the element the way a screen reader would and it fails when your a11y is broken. A getByTestId("submit-btn") finds the node and learns nothing; a missing getByRole("button") is a real accessibility bug the test caught for free. userEvent over fireEvent for the same reason: user.type produces the actual keydown/input/change sequence a human causes; fireEvent can fabricate sequences no user generates.

What earns a test, and what never does

  • Earns: user-visible state transitions — the form goes idle → submitting → error, the error is announced, retry works. Error paths, boundary states, the four fetch states. Anything a user would file a bug about.
  • Never: that useState was called, that a specific child rendered, that a mock of an internal function was invoked. Refactor the component, keep identical behavior, and these tests fail anyway — teaching the suite that green means "nothing changed internally," which is exactly backwards.
  • Never: giant snapshots. expect(container).toMatchSnapshot() on a whole page rots — the diff becomes noise and the test gets --u-updated into meaninglessness without anyone reading it.
// what a real RTL test looks like end-to-end
it("shows the server error and keeps the draft", async () => {
  server.use(http.post("/api/comment", () => new HttpResponse(null, { status: 500 })));
  render(<CommentForm />);
  await user.type(screen.getByLabelText(/comment/i), "ship it");
  await user.click(screen.getByRole("button", { name: /post/i }));
  expect(await screen.findByRole("alert")).toHaveTextContent(/try again/i);
  expect(screen.getByLabelText(/comment/i)).toHaveValue("ship it");
});

Three things are being asserted there that an implementation test would miss: the error surfaces as an alert role (announced by screen readers — the a11y contract is the test), the request actually reached the endpoint msw mocked, and the user's input survived the failure. Refactor the component's internals completely — swap useState for useReducer, split it into children — and the test still passes, because the user-facing behavior didn't change. That is the property you're paying for.

The boundary you mock is the network

Testing Library plus Vitest/Jest covers component logic; mock service worker (msw) intercepts fetch at the network so tests assert "given a 500 response, the error state renders" — not "the internal fetch mock was called." For the flows where the DOM isn't enough — real navigation, real browser APIs — Playwright drives an actual browser and finds the bugs jsdom's approximation never will. The split in practice: Vitest + Testing Library + msw for component behavior, Playwright for a handful of critical end-to-end paths (signup, checkout) — not a pyramid, a chosen set.

Field notes

  • getByRole coverage doubles as an a11y smoke test — a component you can't query by role is usually a component with a semantic problem.
  • StrictMode's double-render will expose a test that relied on effects running once; write assertions about final state, not about how many times anything ran.
  • A test that can't describe the user action it's simulating in one sentence is testing the wrong layer.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.