vitest-dev/vitest · error · TypeError

You must provide an array or set to ${matcherHint('.toBeOneO

Error message

You must provide an array or set to ${matcherHint('.toBeOneOf')}, not '${typeof expected}'.

What it means

Thrown as a TypeError by the toBeOneOf matcher (ported from jest-extended) when the 'expected' argument is neither an Array nor a Set. toBeOneOf asserts the received value equals one of the provided candidates, so it needs an iterable collection to search.

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 d568f8ce37)

Solutions

  1. Wrap candidates in an array: expect(x).toBeOneOf(['a', 'b']).
  2. Or use a Set: expect(x).toBeOneOf(new Set(['a', 'b'])).
  3. If you have another iterable, spread it into an array first.

Example fix

// before
expect(status).toBeOneOf('active', 'idle') // not an array
// after
expect(status).toBeOneOf(['active', 'idle'])
Defensive patterns

Strategy: type-guard

Validate before calling

function asCollection<T>(v: T[] | Set<T> | unknown): T[] | Set<T> {
  if (Array.isArray(v) || v instanceof Set) return v as T[] | Set<T>
  throw new TypeError(`toBeOneOf needs an array or Set, got ${typeof v}`)
}
expect(x).toBeOneOf(asCollection(candidates))

Type guard

function isArrayOfOrSet<T>(v: unknown): v is T[] | Set<T> {
  return Array.isArray(v) || v instanceof Set
}

Prevention

When it happens

Trigger: Calling expect(x).toBeOneOf(value) where value is a primitive, plain object, map, or any non-array/non-set. The matcher branches on Array.isArray then instanceof Set and throws in the else.

Common situations: Passing a comma-separated list as a single value, an object literal instead of an array, or a Map/Iterable that isn't a Set; refactoring a value from an array to something else and forgetting to update the assertion.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/3a01f052e9874f33.json. Report an issue: GitHub.