Best pattern for checking for absence of Element #6560
-
Just started using Vitest 2.1.0, and found a eccentricity that made me stop andcheck I'm not missing something. The documented way of checking for the existence of an element is the following await expect.element(page.getByText('Some Text')).toBeInTheDocument(); the standard Vitest assertion pattern suggests that to check for the absence of an element is the following // \/-----changed
await expect.element(page.getByText('Some Text')).not.toBeInTheDocument(); However this ends up throwing an error, rather than failing the test
Looking around, this is a known issue with testing-library/jest-dom#391 (comment) The https://github.com/testing-library/jest-dom?tab=readme-ov-file#tobeinthedocument However the query selectors are not exposed by Vitest. The solution I have come to is expect(page.getByTestId('path').query()).toBeNull(); but that seems like a big departure from the other pattern for what is essentially the inverse. Is there a better way of doing this? The docs don't seem to acknowledge this as an issue, so I want to check |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
You can do something like this: await expect.poll(() => page.getByTestId('path').query()).not.toBeInTheDocument() |
Beta Was this translation helpful? Give feedback.
You can do something like this: