Skip to content

feat(browser): support toBeInViewport utility method to assert element is in viewport or not #8234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 14, 2025

Conversation

Shinyaigeek
Copy link
Contributor

Description

Close: #7650

This is a suggested feature in that ticket. toBeInViewport is implemented in playwright, and this is port to vitest

Hi team 👋 I am a one of vitest fun, and I wanted to enhance vitest feature.
As a first step, I implemented toBeInViewport feature in vitest/browser package which is suggested in #7650

Perhaps, I could overtake some context, so please feel free to point out that if there is something I overtake. Additionaly, regarding issue #7650, some time has passed since its creation. If there has been an internal decision not to proceed with this feature, please don't hesitate to close the issue.

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.

Tests

  • Run the tests with pnpm test:ci.

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:.

Copy link

netlify bot commented Jun 29, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 2071baf
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6863d28586befd0008ae6dba
😎 Deploy Preview https://deploy-preview-8234--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.

@Shinyaigeek Shinyaigeek marked this pull request as draft June 29, 2025 17:45
@Shinyaigeek
Copy link
Contributor Author

hmm...some tests related to my work seems to be failing, I will see later 🙇

…t is in viewport or not

Close: vitest-dev#7650. This is a suggested feature in that ticket.
toBeInViewport is implemented in playwright, and this is port to vitest
@Shinyaigeek Shinyaigeek force-pushed the support-tobeinviewport branch from 35e02b4 to e4a1aaa Compare June 30, 2025 15:31
sheremet-va
sheremet-va previously approved these changes Jun 30, 2025
Copy link
Member

@sheremet-va sheremet-va left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, just docs missing

@Shinyaigeek
Copy link
Contributor Author

Shinyaigeek commented Jun 30, 2025

Thank you for reviewing! some tests have still been failing on CI...(but in my local environment, these tests pass 🤔 ) Let me more investigating...
I forgot about the document page, so I'll push that later!

@Shinyaigeek
Copy link
Contributor Author

Thank you for reviewing! some tests have still been failing on CI...(but in my local environment, these tests pass 🤔 ) Let me more investigating...

I understood, I forgot to run pnpm build before testing 🤦

I've fixed the tests to pass on browsers other than Firefox. However, before I tackle the docs and Firefox tests, I have a question 🙇

I've implemented toBeInViewport method based on Intersection Observer API for testing in a Playwright-compliant way.

The issue is that the callback function passed to the Intersection Observer API executes after the assertion within a synchronous process. Consequently, when expect(element).toBeInViewport() runs, the callback(=check the element is in viewport or not) hasn't fired yet, leading to an error.

To work around this, I've tentatively implemented a Promise-based solution in eb29fd7. This approach, however, inevitably leads to a usage pattern like await expect(element).toBeInViewport().

Is this API design acceptable? I feel it might be fine because ExpectationResult itself encapsulates a Promise, but I haven't found other examples, so I wanted to ask.

@sheremet-va
Copy link
Member

Is this API design acceptable? I feel it might be fine because ExpectationResult itself encapsulates a Promise, but I haven't found other examples, so I wanted to ask.

Yes, it is fine to always return a Promise

@Shinyaigeek Shinyaigeek force-pushed the support-tobeinviewport branch 3 times, most recently from 3f8a755 to 23f347e Compare July 1, 2025 13:28
@Shinyaigeek Shinyaigeek force-pushed the support-tobeinviewport branch from 23f347e to 5f2b411 Compare July 1, 2025 14:35
@Shinyaigeek Shinyaigeek marked this pull request as ready for review July 1, 2025 15:07
@Shinyaigeek
Copy link
Contributor Author

@sheremet-va

Thank you for reviewing and your answer!
I fixed and tests related to toBeInViewport are passed!(currently, one testing GHA workflow is failing but it seems not related to toBeInViewport)

Also, I wrote document about toBeInViewport in 5f2b411.

so PR is ready for review! Please review this if you have time!

@Shinyaigeek Shinyaigeek requested a review from sheremet-va July 1, 2025 15:10
@sheremet-va sheremet-va merged commit ceed5b6 into vitest-dev:main Jul 14, 2025
14 of 15 checks passed
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.

Implement toBeInViewport assertion
2 participants