We currently have tests that use getByLabelText in the tests when other selector methods would suffice.
Proposed Behavior/Feature:
We should convert getByLabelText tests to something less fragile like getByRole/TestId.
Rationale:
Relying on LabelText for anything other than rare cases where it is necessary make tests fragile and possibly can imply that adding labels is the way to select elements.
Using getByRole in particular can also reinforce testing of functionality. For instance, in testing that a button maintains the expected accessible name, selecting it via Role of button helps reinforce that we are indeed selecting for a button, whereas selecting by LabelText can result in a false positive if the element loses the button role. This could help stop significant accessibility regressions.
Because label texts can change but functionality should change much more seldom, this also makes maintenance of the tests much easier.
Proposed Implementation (if applicable):
For instances where selecting by role is applicable, do so.
In instances where that doesn't work, giving elements a data-testid will give some of the same benefits and not rely so much on implementation details.
Describe the Current Behavior/Feature:
We currently have tests that use
getByLabelText
in the tests when other selector methods would suffice.Proposed Behavior/Feature:
We should convert
getByLabelText
tests to something less fragile likegetByRole/TestId
.Rationale:
Relying on
LabelText
for anything other than rare cases where it is necessary make tests fragile and possibly can imply that adding labels is the way to select elements.Using
getByRole
in particular can also reinforce testing of functionality. For instance, in testing that a button maintains the expected accessible name, selecting it viaRole
ofbutton
helps reinforce that we are indeed selecting for a button, whereas selecting byLabelText
can result in a false positive if the element loses thebutton
role. This could help stop significant accessibility regressions.Because label texts can change but functionality should change much more seldom, this also makes maintenance of the tests much easier.
Proposed Implementation (if applicable):
For instances where selecting by role is applicable, do so.
In instances where that doesn't work, giving elements a
data-testid
will give some of the same benefits and not rely so much on implementation details.Example of existing test
See https://testing-library.com/docs/queries/byrole/ for more info on more specific selection using the
name
option.