You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature] getByRole should default to exact: true #27483
getByRole is most often used with the name option to query based on the target's accessible name -- a great practice for ensuring your elements have accessible labels. However, the name option is (unexpectedly I think) a case-insensitive substring match by default:
By default, matching is case-insensitive and searches for a substring, use exact to control this behavior.
I would expect that most developers would use a RegExp when they actually want a lowercase substring match, ie name: /address/i. I think the expected behavior for strings is typically an exact match.
This less-strict than expected behavior can easily lead to accessibility bugs slipping through the tests. For example, say you use a pencil icon component that sets the alt text / label of the icon to "pencil". This is pretty normal, since if you use that icon in prose you'd want it to have some alt text. But then say you use it in a button along with the visible text "Edit", ie:
<button><pencil-icon/>
Edit
</button>
This button will be located by getByRole("button", {name: "Edit"}) so most would assume the button is labelled as expected. But the actual label for the button is "pencil Edit", which doesn't make any sense for a screen reader user.
I think that this API should default to setting the exact option to true:
exact: Whether name is matched exactly: case-sensitive and whole-string... Ignored when name is a regular expression. Note that exact match still trims whitespace.
This would be a breaking change, but I think it would be worth it to make the library safer. If we wanted to avoid the breaking change, maybe a global setting to control this instead?
Activity
pavelfeldman commentedon Oct 6, 2023
We decided to keep our text matchers case-insensitive by default and do not intend to change it.
test(e2e): use exact name matching on getByRole across all specs
test(e2e): use exact name matching on getByRole across all specs (#61)