[Jest] Avoid using data-testid in test
March 30, 2022
Why?
When writing tests using Jest, your test should resemble how users interact with your code (component, page, etc.) as much as possible.
With that in mind, using data-testid
and getByTestId
opposes the idea because user cannot see or hear the interaction, so this is always the last resource.
Some recommendation: getByRole
, getByText
, getByLabelText
and getByPlaceholderText
.
Reference
https://testing-library.com/docs/queries/about/#priority