Reputation: 1598
I have a test where I use getByRole
to verify that an element exists in the page. However, I have another test where I expect the element to be missing. I thought this would be pretty straightforward using the .not
syntax, but when I try I get an error.
Things I've tried:
expect(getByRole('timestamp')).not.toBeInTheDocument();
expect(getByRole('timestamp')).toBeNull;
expect(getByRole('timestamp')).toThrowError();
... and more...
The error I get:
TestingLibraryElementError: Unable to find an accessible element with the role "timestamp"
How can I verify that an element with a given role isn't present in the document?
Upvotes: 7
Views: 3426
Reputation: 1598
Thanks to this blog post, I found out that you can use queryByRole
instead of getByRole
to test for cases where you expect the element to be missing.
The final version looks like this:
expect(queryByRole('timestamp')).not.toBeInTheDocument();
Upvotes: 6