Skip to content

Conversation

@pabloleonalcaide
Copy link

Description

Adds support for iterables (Set, Map, generators, etc.) to the toBeOneOf matcher, expanding beyond just arrays as requested.

Currently, toBeOneOf only accepts arrays. However, Sets and other iterables are common in JavaScript and forcing users to convert them to arrays ([...mySet]) is unnecessary.

Resolves #8902.

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

Add support for any iterable (Set, Map.keys(), Map.values(), generators, etc.)
in the toBeOneOf matcher, not just arrays.

- Accept Array<T> | Iterable<T> as parameter
- Exclude strings from iterable check to avoid confusion
- Add comprehensive tests for Set, Map, generators, TypedArrays
- Update TypeScript types and JSDoc
- Update documentation with concise examples

close vitest-dev#8902
@netlify
Copy link

netlify bot commented Nov 3, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit f6bb7fd
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6908c60d8a62c9000882d315
😎 Deploy Preview https://deploy-preview-8921--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
Copy link
Contributor

tim-we commented Nov 4, 2025

Hi there, did you see that there was already another MR for #8902 open?
If all iterables should be supported, this is the better implementation, but I am not sure if that makes sense. I wonder for example if putting a Map into this method makes sense and what the expected behavior would be. Array.from(map) would be an array of the map entries and compare the received value against that.

@pabloleonalcaide
Copy link
Author

Hi there, did you see that there was already another MR for #8902 open? If all iterables should be supported, this is the better implementation, but I am not sure if that makes sense. I wonder for example if putting a Map into this method makes sense and what the expected behavior would be. Array.from(map) would be an array of the map entries and compare the received value against that.

Hi!
Thanks for the feedback! My goal with this change was to allow any iterable (like Set, Map.values(), generators, etc.), not just Set. But you're right — the behavior with Map could not be very clear.

I can update the PR to make the supported iterable types explicit and add a test case for Map to define how it should behave. Does that sound good to you?

@tim-we
Copy link
Contributor

tim-we commented Nov 5, 2025

This is not for me to decide, I am just the one who opened the issue. I have a use case for supporting Sets and for other iterables besides Array I don't have one (right now). Maybe this should have been discussed in the issue before opening a second PR?

Anyway I'm happy when either one of the PRs gets merged, as long as Sets are supported in the future :)

@pabloleonalcaide
Copy link
Author

Yes, probably. I didn't actually see the first PR when I saw the issue, and once it was open, I thought it would be interesting to share this second approach.
In any case, it will be great to see the functionality of tobeoneof expanded.

Cheers!

@pabloleonalcaide
Copy link
Author

After seeing that this pull request has been merged, I suspect mine is no longer necessary, correct?
I'll keep it open until you confirm, thank you.

@sheremet-va
Copy link
Member

I think we can close this now, yes.

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

3 participants