vitest-dev/vitest · error · TypeError
You must provide an array to ${this.toString()}, not '${type
Error message
You must provide an array to ${this.toString()}, not '${typeof this.sample}'. What it means
Thrown as a TypeError inside ArrayContaining.asymmetricMatch when this.sample is not an array. ArrayContaining asserts the received value contains all expected elements, so the expected sample must be an array.
Source
Thrown at packages/expect/src/jest-asymmetric-matchers.ts:201
}
toString() {
return `Object${this.inverse ? 'Not' : ''}Containing`
}
getExpectedType() {
return 'object'
}
}
export class ArrayContaining<T = unknown> extends AsymmetricMatcher<Array<T>> {
constructor(sample: Array<T>, inverse = false) {
super(sample, inverse)
}
asymmetricMatch(other: Array<T>, customTesters?: Array<Tester>): boolean {
if (!Array.isArray(this.sample)) {
throw new TypeError(
`You must provide an array to ${this.toString()}, not '${typeof this
.sample}'.`,
)
}
const result
= this.sample.length === 0
|| (Array.isArray(other)
&& this.sample.every(item =>
other.some(another =>
equals(item, another, customTesters),
),
))
return this.inverse ? !result : result
}
toString() {View on GitHub (pinned to d568f8ce37)
Solutions
- Wrap the elements in an array: expect.arrayContaining([1, 2]).
- Spread an iterable into an array: expect.arrayContaining([...set]).
- Use objectContaining or stringContaining for non-array semantics.
Example fix
// before
expect(items).toEqual(expect.arrayContaining('alpha'))
// after
expect(items).toEqual(expect.arrayContaining(['alpha'])) Defensive patterns
Strategy: type-guard
Validate before calling
function asArray<T>(v: unknown): T[] {
if (!Array.isArray(v)) throw new TypeError(`arrayContaining needs an array, got ${typeof v}`)
return v as T[]
}
expect(items).toEqual(expect.arrayContaining(asArray(expected))) Type guard
function isArrayOf<T>(v: unknown): v is T[] {
return Array.isArray(v)
} Prevention
- Always wrap elements in [] for arrayContaining.
- Spread non-array iterables ([...set]) before passing.
- Type expected values as unknown[] in shared helpers.
When it happens
Trigger: Constructing expect.arrayContaining(value) where value is not an array (a single item, a Set, a plain object). The constructor stores it but matching throws on Array.isArray(this.sample) === false.
Common situations: Passing a single element instead of an array; passing a Set/Map where an array was expected; refactor changing the shape of the expected value.
Related errors
- Expected is not a string
- You must provide an object to ${this.toString()}, not '${typ
- any() expects to be passed a constructor function. Please pa
- Expected is not a String or a RegExp
- Expected is not a Number
AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03).
Data as JSON: /data/errors/da3fa524a506af87.json.
Report an issue: GitHub.