Skip to content

Conversation

@tim-we
Copy link
Contributor

@tim-we tim-we commented Nov 2, 2025

Description

This PR adds Set support to the .toBeOneOf method.

const allowedFruits = new Set(['apple', 'banana', 'orange'])
expect(fruit).toBeOneOf(allowedFruits)

Resolves #8902

I find it quite strange that the test passes if the given array is empty. For consistency the Set implementation behaves the same. Is this really intentional? If so I would adjust the documentation. If not I can change it in a follow up PR.

Also I did not use the vitest internal equals method so something like

expect('apple').toBeOneOf(new Set([expect.any(String)]))

would fail. I don't think toBeOneOf will ever be used like that, therefore I just used the native Set.has method. Does that work for you?

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.

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

@netlify
Copy link

netlify bot commented Nov 2, 2025

Deploy Preview for vitest-dev ready!

Name Link
🔨 Latest commit ba03cd8
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6913ac950f00b00008a7802c
😎 Deploy Preview https://deploy-preview-8906--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.

@tim-we tim-we marked this pull request as ready for review November 2, 2025 14:39
@sheremet-va
Copy link
Member

sheremet-va commented Nov 3, 2025

would fail. I don't think toBeOneOf will ever be used like that, therefore I just used the native Set.has method. Does that work for you?

I would expect toBeOneOf([expect.any()]) to work the same as toBeOneOf(new Set([expect.any()]))

@tim-we
Copy link
Contributor Author

tim-we commented Nov 3, 2025

I would expect toBeOneOf([expect.any()]) to work the same as toBeOneOf(new Set([expect.any()]))

@sheremet-va I changed the implementation to behave the exact same way as the array.

No idea why there are 4 checks failing, is that to be expected at the moment or is this somehow caused by my changes?

sheremet-va
sheremet-va previously approved these changes Nov 5, 2025
@tim-we tim-we requested a review from sheremet-va November 12, 2025 17:33
@sheremet-va sheremet-va merged commit a415d03 into vitest-dev:main Nov 12, 2025
12 of 14 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.

Support sets as argument to expect.toBeOneOf

2 participants