Skip to content

Conversation

@jnizet
Copy link

@jnizet jnizet commented Nov 6, 2025

Description

When using an assertion such as expect(page.getByTestId('not-existing')).not.toBeVisible(), the assertion used to fail because the assertion tried to get the element of the locator, which threw an exception. After this fix, the assertion will pass, because an element that does not exist (and which is thus not in the document) is de facto not visible. It allows the developer to use not.toBeVisible() without having to care if the element is hidden in the DOM or removed from the DOM.

See #8946 (comment)

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci. but so many tests are failing that can't possibly come from the tiny fix I introduced

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

When using an assertion such as `expect(page.getByTestId('not-existing')).not.toBeVisible()`, the assertion used to fail because the assertion tried to get the element of the locator, which threw an exception.
After this fix, the assertion will pass, because an element that does not exist (and which is thus not in the document) is de facto not visible.
It allows the developer to use `not.toBeVisible()` without having to care if the element is hidden in the DOM or removed from the DOM.
@netlify
Copy link

netlify bot commented Nov 6, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 8eec3fe
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/690c6d5a68ca3a0008b2fffa
😎 Deploy Preview https://deploy-preview-8952--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@jnizet
Copy link
Author

jnizet commented Nov 6, 2025

I just realized that this fix only partially fixes the issue.

An assertion such as expect(page.getByTestId('not-existing')).not.toBeVisible() will pass, but unfortunatly, an assertion such as await expect.element(page.getByTestId('not-existing')).not.toBeVisible() will still fail, because expect.element(locator) always tries to resolve the element of the locator, instead of repeating the assertion on the locator until it succeeds.

I guess I'll need to look into how the assertion .not.toBeInTheDocument() works, but I wonder why expect.element(locator).toXxx() is equivalent to retryUntilItSucceeds(() => expect(locator.element()).toXxx()) instead of being equivalent to retryUntilItSucceeds(() => expect(locator).toXxx()).

@jnizet
Copy link
Author

jnizet commented Nov 6, 2025

I added a second commit which makes it work with expect(page.getByTestId('not-existing')).not.toBeVisible()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant