vitest-dev/vitest · error · TypeError
You must provide an array to
Error message
You must provide an array to ${this.toString()}, not '${typeof this.sample}'. What it means
ArrayContaining's asymmetricMatch throws a TypeError if this.sample is not an array. Like objectContaining, this fires at match time when the matcher is evaluated, not at construction. An empty array passes against any array (vacuous containment).
Solutions
- Wrap the expected items in an array: expect.arrayContaining([1, 2, 3]).
- Spread iterable sources: expect.arrayContaining([...setOfIds]).
- For a single item, still use an array literal of length 1.
Example fix
// before expect(ids).toEqual(expect.arrayContaining(42)) // after expect(ids).toEqual(expect.arrayContaining([42, 43]))
Defensive patterns
Strategy: type-guard
Validate before calling
function asArrayContaining(sample) {
if (!Array.isArray(sample)) {
throw new TypeError('expect.arrayContaining needs an array')
}
return expect.arrayContaining(sample)
} Type guard
function isUnknownArray(v): v is unknown[] {
return Array.isArray(v)
} Prevention
- Pass an array literal to expect.arrayContaining.
- Spread sets/iterables into an array first.
- Note this fires at match time, so unit-test the matcher in isolation.
When it happens
Trigger: expect.arrayContaining(sample) built with a non-array sample, then used in toEqual/toEqual-like assertions.
Common situations: Passing a single value instead of an array (arrayContaining(1) instead of arrayContaining([1])); passing a Set (use [...set]); passing an object where a list was intended.
Related errors
- any() expects to be passed a constructor function. Please…
- Expected is not a Number
- Expected is not a string
- Expected is not a String or a RegExp
- Precision is not a Number
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/da3fa524a506af87.
Report an issue: GitHub.
Appendix: 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 1fa9837ec2)