React Testing Library getByRole vs. getByText
September 3, 2025•110 words
getByRole()
roles are automatically inferred based on ARIA specs, or they can be manually set
it's generally preferred because it's more robust, it mirrors more accurately the UX and it tests accessible elements (accessibility tree)
the returned objects can be queried by accessible names
it's less performant especially in large DOMs
--
Resources
ByRole | Testing Library (2024). https://testing-library.com/docs/queries/byrole.
ARIA in HTML (2025). https://www.w3.org/TR/html-aria/#docconfo...
Read post