Queries

Every Testing Library 221,825 query is built from three choices combined into one camel-case name: {get|query|find}[All]By{Role|LabelText|PlaceholderText|Text|DisplayValue|AltText|Title|TestId}.

What each query prefix does, by number of matches
Prefix No match One match Several Returns
getBy throws the node throws node
queryBy null the node throws node or null
findBy throws after 1 s the node throws Promise

The All variants — getAllBy, queryAllBy, findAllBy — behave the same way except that several matches are the expected case, and each returns an array. The choice is mechanical: getBy for something that must be there now, because the thrown error beats expect(null); queryBy only with not.toBeInTheDocument(), the one prefix that can report absence; findBy for anything that arrives after an await.

For the suffix the order is ByRole with a name, then ByLabelText, then ByText, with ByTestId last. That is not style advice: a query that finds a node by accessible role and name can only succeed if assistive technology can find it too (Accessibility). When the role is wrong:

Output of 122
 × src/LoginForm.test.jsx > has a heading 141ms
   → Unable to find an accessible element with the role "heading" and name `/sign in/i`
Here are the accessible roles:
  textbox:
  Name "Email":
  <input id="email" name="email" placeholder="you@example.com" />
  ... (each attribute printed on its own line; the button, name "Sign in", follows)

The error lists every role in the tree with its accessible name, usually enough to fix the test without opening the component. Note what is missing: <input type="password"> has no ARIA role at all, so no getByRole call can reach it — which is why getByLabelText('Password') exists.

String matching is exact and whitespace-normalized; pass a regular expression for anything more precise, since /^Save$/ cannot match "Save as draft". within(node) rebinds the query set to one subtree, which saves you in a table where getByRole('button', { name: 'Delete' }) finds twelve.