vitest-dev/vitest · error · TypeError
You must provide an array or set to
Error message
You must provide an array or set to ${matcherHint('.toBeOneOf')}, not '${typeof expected}'. What it means
The toBeOneOf matcher requires its expected argument to be an Array or a Set; any other type raises a TypeError before equality is evaluated. An empty Array/Set passes by default (matches anything), but a primitive or plain object is rejected.
Solutions
- Wrap the candidate value(s) in an array: toBeOneOf(['active']).
- Use a Set when testing membership against a large or deduplicated collection.
- If you meant a single value, use toBe(value) instead.
Example fix
// before
expect(status).toBeOneOf('active')
// after
expect(status).toBeOneOf(['active', 'idle', 'blocked']) Defensive patterns
Strategy: type-guard
Validate before calling
function assertCandidates(v) {
if (!Array.isArray(v) && !(v instanceof Set)) {
throw new TypeError('toBeOneOf expects an Array or Set')
}
} Type guard
function isArrayOrSet(v): v is Array<unknown> | Set<unknown> {
return Array.isArray(v) || v instanceof Set
} Prevention
- Always pass an array literal to toBeOneOf, even for a single value.
- Use toBe(value) when only one value is acceptable.
- Lint for toBeOneOf called with string/number literals.
When it happens
Trigger: expect(actual).toBeOneOf(expected) where expected is a string, number, object literal, Map, or any non-Array/non-Set value.
Common situations: Passing a single allowed value unwrapped (toBeOneOf('active') instead of toBeOneOf(['active'])); confusing toBeOneOf with toBe; passing a Map where a Set was intended.
Related errors
- toContain() expected a DOM node as the argument, but got
- .toMatch() expects to receive a string, but got
- any() expects to be passed a constructor function. Please…
- Exact option does not support RegExp expected class names
- expect.customEqualityTesters: Must be set to an array of…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/3a01f052e9874f33.
Report an issue: GitHub.
Appendix: source
Thrown at packages/expect/src/custom-matchers.ts:46
},
toBeOneOf(actual: unknown, expected: Array<unknown> | Set<unknown>) {
const { equals, customTesters } = this
const { printReceived, printExpected, matcherHint } = this.utils
let pass: boolean
if (Array.isArray(expected)) {
pass = expected.length === 0
|| expected.some(item =>
equals(item, actual, customTesters),
)
}
else if (expected instanceof Set) {
pass = expected.size === 0 || expected.has(actual) || [...expected].some(item => equals(item, actual, customTesters))
}
else {
throw new TypeError(
`You must provide an array or set to ${matcherHint('.toBeOneOf')}, not '${typeof expected}'.`,
)
}
return {
pass,
message: () =>
pass
? `\
${matcherHint('.not.toBeOneOf', 'received', '')}
Expected value to not be one of:
${printExpected(expected)}
Received:
${printReceived(actual)}`
: `\
${matcherHint('.toBeOneOf', 'received', '')}
View on GitHub (pinned to 1fa9837ec2)