Skip to content

testing-library

Use when testing React components with @testing-library/react — DOM queries, user interactions, async rendering, and accessibility assertions. Also use for React 19 Suspense testing patterns, Vitest integration, and Storybook portable story reuse.

ModelSource
sonnetpack: frontend
Full Reference

┏━ 🔧 testing-library ━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Use when testing React components with @testin… ┃ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

Test components like users use them — not like developers built them.

PackageVersionRole
@testing-library/reactv16.xCore render + screen queries
@testing-library/dompeer depMust install explicitly (v16 breaking change)
@testing-library/user-eventv14.xRealistic user interaction simulation
@types/react-domlatestTypeScript type checking

Install: npm install --save-dev @testing-library/react @testing-library/dom @testing-library/user-event @types/react-dom

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use queries in this order. Higher = more accessible, more resilient, more like a real user.

  1. getByRole — #1 always. Matches ARIA roles. Tests accessibility implicitly.
  2. getByLabelText — form fields. Mirrors how screen readers navigate.
  3. getByPlaceholderText — when no label exists (not ideal UX, but testable).
  4. getByText — non-interactive elements: headings, paragraphs, spans.
  5. getByDisplayValue — filled form elements (input/select/textarea with current value).
  6. getByAltText — images and area elements.
  7. getByTitle — title attribute. Inconsistent screen reader support — use sparingly.
  8. getByTestId — last resort only. Use when no semantic query works.

Test like a user navigates — not like a developer inspects the DOM.

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

FileWhat it covers
setup.mdInstall command, Vitest config, Jest config, Next.js + Vite + Storybook notes
queries.mdQuery variants (getBy/queryBy/findBy), priority guide, all query types with examples
events.mduser-event v14 — click, keyboard, type, pointer, clipboard, select, tab
async.mdwaitFor, findBy*, act(), React 19 Suspense patterns, concurrent features
patterns.mdCustom render with providers, forms, modals, navigation, context, MSW, Storybook portable stories
accessibility.mdjest-axe/vitest-axe, keyboard nav, ARIA live regions, logRoles, jest-dom matchers

Usage: Read the reference file matching your current task from the index above. Each file is self-contained with code examples and inline gotchas.