jestjs/jest · 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

ArrayContaining.asymmetricMatch checks that this.sample is an array before iterating; if a non-array was passed to expect.arrayContaining it throws TypeError at match time. The check is deferred to match time (not the constructor) so the matcher object can still be constructed and only fails when actually compared against a received value.

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

Solutions

  1. Wrap the expected items in an array: expect.arrayContaining([5]) not expect.arrayContaining(5).
  2. If the value comes from a variable, ensure it is an array before passing: Array.isArray(x) ? x : [x].
  3. Re-read the matcher name — arrayContaining specifically checks membership within arrays.

Example fix

// before
expect(received).toEqual(expect.arrayContaining(5));

// after
expect(received).toEqual(expect.arrayContaining([5]));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!Array.isArray(sample)) {
  throw new TypeError('expect.arrayContaining needs an array');
}
expect.arrayContaining(sample);

Type guard

function isArrayOfUnknown(x: unknown): x is unknown[] {
  return Array.isArray(x);
}

Try / catch

// throws at match time; guard the sample at construction with Array.isArray

Prevention

When it happens

Trigger: Calling expect.arrayContaining with a non-array sample (a primitive, an object, undefined) and then using that matcher in a comparison such as toEqual, toHaveBeenCalledWith, or objectContaining where it gets evaluated.

Common situations: Passing a single value instead of a list: expect.arrayContaining(5) instead of expect.arrayContaining([5]); spreading a non-iterable; a variable that was expected to be an array but is undefined.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/71fd3635eef2f006.json. Report an issue: GitHub.