Use a custom text match function that only matches visible text.
function matchVisible(text) {
return (content, element) => content.startsWith(text) && element.getAttribute('aria-hidden') !== 'true';
}
test('everything is fine', () => {
render(<MyApp />);
const error = screen.queryByText(matchVisible('flagrant error'));
expect(error).toBeNull();
});
Reference: https://github.com/testing-library/dom-testing-library/issues/929#issuecomment-1571823720