queryByRole
queryByRole is a query function from DOM Testing Library (used by React Testing Library) that locates a single element by its ARIA role. It searches the rendered DOM for elements with the specified role and optional accessible name, and returns the first match. If no element matches, it returns null. It does not throw on a missing element, making it useful for tests that need to assert absence or optional presence.
Usage and options: queryByRole can be called with a container or via screen, e.g., screen.queryByRole('button') or
Relation to other queries: queryByRole is part of a family that includes getByRole, queryAllByRole, and findByRole.
Accessibility emphasis: queryByRole leverages the accessibility tree to promote tests that reflect how users interact with