Jest - how to test if a component does not exist?

.contains receives a React Node or array of Nodes as an argument. Instead, use .find:

expect(wrapper.find('selector').exists()).toBeTruthy()

If you're using react-testing-library (I know the OP wasn't but I found this question via web search) then this will work:

expect(component.queryByText("Text I care about")).not.toBeInTheDocument();

You can query by Text, Role, and several others. See docs for more info.

Note: queryBy* will return null if it is not found. If you use getBy* then it will error out for elements not found.


You can use enzymes contains to check if the component was rendered:

expect(component.contains(<ComponentName />)).toBe(false)

Tags:

Jestjs

Enzyme