React Testing Library getByRole vs. getByText

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


You'll only receive email when they publish something new.

More from Marcella Malune
All posts