How do you test for the non-existence of an element using jest and react-testing-library?

I have a component library that I’m writing unit tests for using Jest and react-testing-library. Based on certain props or events I want to verify that certain elements aren’t being rendered.

getByText, getByTestId, etc throw and error in react-testing-library if the element isn’t found causing the test to fail before the expect function fires.

How do you test for something not existing in jest using react-testing-library?

9 Answers
9

Leave a Comment