CodePark/ questions / Testing
START PRACTICINGPRACTICE
Frontend question · React › Testing

Following React Testing Library's guidance, which query is the best way to find a Submit button?

screen.getByRole('button', { name: /submit/i })

getByRole('button', { name: /submit/i }) is best. It finds the element the way users and screen readers see it. It also fails if the button is not accessible.

getByText('Submit') is the tempting wrong answer. It would also match text that is not a button. A CSS class like .btn-primary is an implementation detail. Test IDs are a last resort.