ByTestId

Despite all previous lessons encouraging you to use React Testing Library query functions that act like real users... the reality is that sometimes that is very hard to do. We might have multiple buttons with the same text, which makes selecting a specific one in tests hard to do.

There is an escape hatch, which gives you the power to mark an element to be selected.

That is getByTestId, which will look for elements with the data-testid attribute.

// <div data-testid="some-test-id" />
getByTestId('some-test-id');

Loading full lesson content & code...

Ready to try running tests for this lesson?