jestjs/jest · 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 / arrayNotContaining build an ArrayContaining matcher whose asymmetricMatch checks that this.sample is an array before testing containment. Although TypeScript types the sample as Array<unknown>, at runtime (plain JS, or a cast) a non-array sample triggers this TypeError at match time. The check exists so the failure is reported clearly instead of producing a confusing `.every` error.
Solutions
- Pass an array literal: `expect.arrayContaining([item1, item2])`.
- If you intended to match an object's subset, use `expect.objectContaining({...})` instead.
- Verify the runtime shape of the value passed (log it / assert Array.isArray) before constructing the matcher.
Example fix
// before
expect(result).toEqual(expect.arrayContaining({ id: 1 })); // object, not array
// after
expect(result).toEqual(expect.arrayContaining([{ id: 1 }]));
// or for object subset:
expect(result).toEqual(expect.objectContaining({ id: 1 })); Defensive patterns
Strategy: type-guard
Validate before calling
function arrayContainingSafe(sample) {
if (!Array.isArray(sample)) {
throw new TypeError('arrayContaining requires an array');
}
return expect.arrayContaining(sample);
} Type guard
function isUnknownArray(v: unknown): v is unknown[] {
return Array.isArray(v);
}
const m = isUnknownArray(sample) ? expect.arrayContaining(sample) : expect.anything(); Prevention
- Always wrap arrayContaining samples in [...].
- Use objectContaining for object subsets, arrayContaining for array contents.
- Log/assert the runtime shape of dynamic samples before building matchers.
When it happens
Trigger: Calling `expect.arrayContaining(value)` where value is not an array — e.g. an object, a single primitive, or undefined. The error surfaces when the matcher actually runs (during toEqual/toStrictEqual), not at construction.
Common situations: Passing an object literal instead of an array; passing a single element instead of a list; mixing up objectContaining and arrayContaining; receiving data from an untyped source whose shape differs from expectation.
Related errors
- any() expects to be passed a constructor function. Please…
- Expected is not a string
- Expected is not a String or a RegExp
- You must provide an object to
- expect.customEqualityTesters: Must be set to an array of…
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/71fd3635eef2f006.
Report an issue: GitHub.
Appendix: source
Thrown at packages/expect/src/asymmetricMatchers.ts:200
toString() {
return 'Anything';
}
// No getExpectedType method, because it matches either null or undefined.
override toAsymmetricMatcher() {
return 'Anything';
}
}
class ArrayContaining extends AsymmetricMatcher<Array<unknown>> {
constructor(sample: Array<unknown>, inverse = false) {
super(sample, inverse);
}
asymmetricMatch(other: unknown) {
if (!Array.isArray(this.sample)) {
throw new TypeError(
`You must provide an array to ${this.toString()}, not '${typeof this
.sample}'.`,
);
}
const matcherContext = this.getMatcherContext();
const result =
this.sample.length === 0 ||
(Array.isArray(other) &&
this.sample.every(item =>
other.some(another =>
equals(item, another, matcherContext.customTesters),
),
));
return this.inverse ? !result : result;
}
View on GitHub (pinned to 8e6d128e4a)