awslabs/llrt · 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.asymmetricMatch requires its sample to be an array and throws a TypeError otherwise, reporting the sample's typeof. This guards against constructing expect.arrayContaining with a scalar or object and silently getting failing/erratic matches.

Solutions

  1. Pass a real array: expect.arrayContaining([1, 2]).
  2. Convert the value with Array.from(value) or value.split(',') when appropriate.
  3. Check the data source — the value may be an object or null where an array was expected.
  4. Use expect.objectContaining for keyed collections instead.

Example fix

// before
expect(ids).toEqual(expect.arrayContaining('1,2'));
// after
expect(ids).toEqual(expect.arrayContaining('1,2'.split(',')));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!Array.isArray(sample)) throw new TypeError('arrayContaining sample must be an array');

Type guard

const isArr = <T>(v: unknown): v is T[] => Array.isArray(v);

Try / catch

try { expect(xs).toEqual(expect.arrayContaining(sample)); } catch (e) { /* inspect sample type and rebuild matcher */ }

Prevention

When it happens

Trigger: expect.arrayContaining(5), expect.arrayContaining('a,b'), or expect.arrayContaining({0:'x'}) — thrown when the matcher runs against a value.

Common situations: Passing a comma-joined string that was never split, passing arguments/iterables that are not real arrays, or API data that is an object map instead of a list.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of awslabs/llrt@742fc00b82 (2026-09-12). Data as JSON: /api/errors/627dc696c7e9622e. Report an issue: GitHub.

Appendix: source

Thrown at llrt_core/src/modules/js/@llrt/expect/jest-asymmetric-matchers.ts:180

  }

  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>) {
    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, []))
        ));

    return this.inverse ? !result : result;
  }

  toString() {
    return `Array${this.inverse ? "Not" : ""}Containing`;
  }

View on GitHub (pinned to 742fc00b82)