Queries and Roles in Testing Library
Learn the priority order of Testing Library queries and why role-based queries are the recommended default.
Testing Library provides several families of queries — getBy*, queryBy*, findBy* — each combined with a selector strategy like Role, LabelText, Text, PlaceholderText, or TestId. The official documentation publishes a deliberate priority order recommending which selector strategy to reach for first, with getByRole at the very top, since it most closely matches how assistive technology and visual users alike perceive an element's purpose.
Query priority is like choosing how to identify someone in a crowd: recognizing them by their actual role and name tag (getByRole) works the same way real people would find them, while relying on a hidden numbered sticker only testers can see (getByTestId) tells you nothing about whether an ordinary visitor could actually locate that person.