[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

feat(expect): add Set support to toBeOneOf (#8906)

Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>

authored by

Tim Weißenfels
Vladimir
and committed by
GitHub
(Nov 12, 2025, 6:38 PM +0100) a415d037 353ee5bb

+28 -12
+6 -2
docs/api/expect.md
··· 379 379 380 380 ## toBeOneOf 381 381 382 - - **Type:** `(sample: Array<any>) => any` 382 + - **Type:** `(sample: Array<any> | Set<any>) => any` 383 383 384 - `toBeOneOf` asserts if a value matches any of the values in the provided array. 384 + `toBeOneOf` asserts if a value matches any of the values in the provided array or set. 385 + 386 + ::: warning EXPERIMENTAL 387 + Providing a `Set` is an experimental feature and may change in a future release. 388 + ::: 385 389 386 390 ```ts 387 391 import { expect, test } from 'vitest'
+14 -8
packages/expect/src/custom-matchers.ts
··· 27 27 } 28 28 }, 29 29 30 - toBeOneOf(actual: unknown, expected: Array<unknown>) { 30 + toBeOneOf(actual: unknown, expected: Array<unknown> | Set<unknown>) { 31 31 const { equals, customTesters } = this 32 32 const { printReceived, printExpected, matcherHint } = this.utils 33 33 34 - if (!Array.isArray(expected)) { 34 + let pass: boolean 35 + 36 + if (Array.isArray(expected)) { 37 + pass = expected.length === 0 38 + || expected.some(item => 39 + equals(item, actual, customTesters), 40 + ) 41 + } 42 + else if (expected instanceof Set) { 43 + pass = expected.size === 0 || expected.has(actual) || [...expected].some(item => equals(item, actual, customTesters)) 44 + } 45 + else { 35 46 throw new TypeError( 36 - `You must provide an array to ${matcherHint('.toBeOneOf')}, not '${typeof expected}'.`, 47 + `You must provide an array or set to ${matcherHint('.toBeOneOf')}, not '${typeof expected}'.`, 37 48 ) 38 49 } 39 - 40 - const pass = expected.length === 0 41 - || expected.some(item => 42 - equals(item, actual, customTesters), 43 - ) 44 50 45 51 return { 46 52 pass,
+2 -2
packages/expect/src/types.ts
··· 129 129 toSatisfy: (matcher: (value: any) => boolean, message?: string) => any 130 130 131 131 /** 132 - * Matches if the received value is one of the values in the expected array. 132 + * Matches if the received value is one of the values in the expected array or set. 133 133 * 134 134 * @example 135 135 * expect(1).toBeOneOf([1, 2, 3]) 136 136 * expect('foo').toBeOneOf([expect.any(String)]) 137 137 * expect({ a: 1 }).toEqual({ a: expect.toBeOneOf(['1', '2', '3']) }) 138 138 */ 139 - toBeOneOf: <T>(sample: Array<T>) => any 139 + toBeOneOf: <T>(sample: Array<T> | Set<T>) => any 140 140 } 141 141 142 142 export interface AsymmetricMatchersContaining extends CustomMatcher {
+6
test/core/test/jest-expect.test.ts
··· 636 636 expect(0).toBeOneOf([0, 1, 2]) 637 637 expect(0).toBeOneOf([expect.any(Number)]) 638 638 expect('apple').toBeOneOf(['apple', 'banana', 'orange']) 639 + expect('apple').toBeOneOf(new Set(['apple', 'banana', 'orange'])) 639 640 expect('apple').toBeOneOf([expect.any(String)]) 641 + expect('apple').toBeOneOf(new Set([expect.any(String)])) 640 642 expect(true).toBeOneOf([true, false]) 641 643 expect(true).toBeOneOf([expect.any(Boolean)]) 642 644 expect(null).toBeOneOf([expect.any(Object)]) ··· 647 649 expect(3).not.toBeOneOf([0, 1, 2]) 648 650 expect(3).not.toBeOneOf([expect.any(String)]) 649 651 expect('mango').not.toBeOneOf(['apple', 'banana', 'orange']) 652 + expect('mango').not.toBeOneOf(new Set(['apple', 'banana', 'orange'])) 650 653 expect('mango').not.toBeOneOf([expect.any(Number)]) 654 + expect('mango').not.toBeOneOf(new Set([expect.any(Number)])) 651 655 expect(null).not.toBeOneOf([undefined]) 652 656 }) 653 657 ··· 655 659 expect(3).toBeOneOf([0, 1, 2]) 656 660 expect(3).toBeOneOf([expect.any(String)]) 657 661 expect('mango').toBeOneOf(['apple', 'banana', 'orange']) 662 + expect('mango').toBeOneOf(new Set(['apple', 'banana', 'orange'])) 658 663 expect('mango').toBeOneOf([expect.any(Number)]) 664 + expect('mango').toBeOneOf(new Set([expect.any(Number)])) 659 665 expect(null).toBeOneOf([undefined]) 660 666 }) 661 667