I have some text like "Message failed to send" that I want to assert will not appear on screen. According to this answer, I should avoid compound logic in my assertions. And according to the React Testing Library guiding principles, I should write tests based on the user experience. So it seems like I should do something like this:
test('it does not display an error', () => {
render(<App />);
expect(screen).not.toDisplayText('failed to send');
});
This matcher doesn't seem to exist, though, and I can't find one equivalent to it.
- I can use
expect(screen.queryByText('fail')).not.toBeInTheDocument()
, which passes if the text is nonexistent but fails if it is nonvisible. - I can use
expect(screen.queryByText('fail')).not.toBeVisible()
, which passes if the text is nonvisible but fails if it is nonexistent.
It seems like "this text does not exist and I don't care why" should be a pretty common query, but I can't find a clean way to test for it. Right now I'm just switching which test I use based on whether I expect the element to be nonexistent or nonvisible, but that feels wrong. Is there a better way?