Concept: Query by Role
Question
Why prefer getByRole over getByTestId in React Testing Library?
Hint
Think about who uses accessibility roles — screen readers and users.
Explanation
Preferring getByRole over getByTestId encourages testing the way users interact with the app. Roles (like 'button', 'textbox', 'heading') are part of the accessibility tree and reflect how screen readers and real users find elements. If you can query by role, your component is accessible. getByTestId adds test-only attributes that don't verify accessibility.
Related Resources
Related Lessons
- React Testing
Testing React components.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.