Testing Library is a family of lightweight testing utilities built on top of DOM Testing Library, designed to test web components as users interact with them rather than testing implementation details. It emphasizes accessibility-first queries (like getByRole) and realistic user interactions (via user-event), making tests more resilient to refactors and closer to actual user behavior. The key insight: if your test resembles how users interact with your app, it gives you more confidence your application works correctly.
What This Cheat Sheet Covers
This topic spans 18 focused tables and 104 indexed concepts, 61 flashcards. Below is a complete table-by-table outline of this topic, spanning foundational concepts through advanced details.
A jump-to index of every table row in this cheat sheet.
An interactive map of every table and concept in this topic.
Table 1: Query Type Variants
Every query in Testing Library is one of these six variants, and picking the right one is half the battle. The choice comes down to three questions: should a missing element throw or return null (get vs. query), do you expect one element or many (the All variants), and is the element there now or arriving asynchronously (find, which returns a retrying promise)?
| Variant | Example | Description | |
|---|---|---|---|
getByRole('button') | • Returns element immediately • throws error if not found or multiple matches • use when element must be present | ||
queryByRole('button') | • Returns element or null • does not throw • use for asserting element absence | ||
await findByRole('button') | • Returns Promise (default 1000ms timeout) • retries until element appears • use for async elements | ||
getAllByRole('listitem') | • Returns array of elements • throws if none found • use for multiple elements that must exist | ||
queryAllByRole('listitem') | • Returns array or empty array • does not throw • use for checking zero or more elements | ||
await findAllByRole('listitem') | • Returns Promise of array • retries until elements appear • use for async multiple elements |