Skip to content
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

feat(browser): introduce expect.element method and bundle jest-dom matchers with @vitest/browser #5910

Merged
merged 5 commits into from
Jun 20, 2024

Conversation

sheremet-va
Copy link
Member

@sheremet-va sheremet-va commented Jun 17, 2024

Description

This PR adds expect.element(element) method which is a shorthand for expect.poll(() => element), but it requires Element:

await expect.element(div).toHaveTextContent('Hello World!')

The main reason why this API exists is to make retriability easier and more intuitive to manage. Previously, the most performant way was to get the element and pass the reference to poll which makes it verbose and not intuitive:

const input = screen.getByRole('input')
await expect.poll(() => input).toHaveValue('')

If you have it like this, the getBy is called multiple times which might be slow depending on the DOM state and the query itself:

await expect.poll(() => screen.getByRole('input')).toHaveValue('')

This PR also bundles jest-dom matchers for Vitest browser mode. I hope we can just import them in the future without the bundle process.

To have types recognize these, users need to add @vitest/browser/providers/playwright or @vitest/browser/providers/webdriverio to their tsconfig file.

TODO:

  • Docs

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

@sheremet-va sheremet-va added the p2-to-be-discussed Enhancement under consideration (priority) label Jun 17, 2024
@sheremet-va sheremet-va changed the title feat(browser): introduce expect.dom method and bundle jest-dom with @vitest/browser feat(browser): introduce expect.dom method and bundle jest-dom matchers with @vitest/browser Jun 17, 2024
@sheremet-va sheremet-va force-pushed the feat/retryibility-dom branch from cafab49 to 79c3485 Compare June 20, 2024 10:32
@sheremet-va sheremet-va removed the p2-to-be-discussed Enhancement under consideration (priority) label Jun 20, 2024
@sheremet-va sheremet-va marked this pull request as ready for review June 20, 2024 11:07
@sheremet-va sheremet-va merged commit 3a96a3d into vitest-dev:main Jun 20, 2024
16 checks passed
@sheremet-va sheremet-va deleted the feat/retryibility-dom branch June 20, 2024 11:08
@sheremet-va sheremet-va changed the title feat(browser): introduce expect.dom method and bundle jest-dom matchers with @vitest/browser feat(browser): introduce expect.element method and bundle jest-dom matchers with @vitest/browser Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

1 participant