Element presence
Loading "Element Presence"
Run locally for transcripts
So far we've covered finding and interacting with elements that are present or will appear on the page. But how do we test for elements that should not render?
This is where you would normally reach for an inverse assertion to get a predictable result and guard yourself from false-positive tests:
const notificationVisiblePromise = vi.waitFor(() => {
expect(notification).toBeVisible()
})
// Instead of asserting that the element is not visible,
// which may lead to false-positive results if it appears after a delay,
// check that the element has never appeared instead.
await expect(notificationVisiblePromise).rejects.toThrow()
Or, alternatively, use the designated
waitForElementToBeRemoved()
utility from React Testing Library:await waitForElementToBeRemoved(notification)
However, there's a more convenient way to do this in Vitest!