jestjs/jest · error · TypeError

You must provide an object to

Error message

You must provide an object to ${this.toString()}, not '${typeof this.sample}'.

What it means

objectContaining / objectNotContaining build an ObjectContaining matcher whose asymmetricMatch requires this.sample to be of type 'object' (and rejects arrays as the sample). A non-object sample (string, number, undefined) throws a TypeError at match time. The guard gives a clear message rather than failing inside the key-iteration loop.

Solutions

  1. Pass a plain object describing the expected subset: `expect.objectContaining({ id: 1 })`.
  2. If you intended to match array contents, use `expect.arrayContaining([...])`.
  3. Confirm the sample is a non-null, non-array object at runtime before constructing the matcher.

Example fix

// before
expect(result).toEqual(expect.objectContaining('id')); // string, not object

// after
expect(result).toEqual(expect.objectContaining({ id: 1 }));
Defensive patterns

Strategy: type-guard

Validate before calling

function objectContainingSafe(sample) {
  if (sample === null || typeof sample !== 'object' || Array.isArray(sample)) {
    throw new TypeError('objectContaining requires a plain object');
  }
  return expect.objectContaining(sample);
}

Type guard

function isPlainObject(v: unknown): v is Record<string | symbol, unknown> {
  return typeof v === 'object' && v !== null && !Array.isArray(v);
}

const m = isPlainObject(sample) ? expect.objectContaining(sample) : expect.anything();

Prevention

When it happens

Trigger: Calling `expect.objectContaining(value)` where value is a primitive (string/number), undefined, or an array. The throw occurs when the matcher executes during an assertion like toEqual.

Common situations: Passing a string or number where an object schema was intended; passing an array (which is technically typeof 'object') expecting key matching; supplying undefined from an optional builder.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/e815340ad4f4c590. Report an issue: GitHub.

Appendix: source

Thrown at packages/expect/src/asymmetricMatchers.ts:259

    return `${this.inverse ? 'Not' : ''}ArrayOf`;
  }

  override getExpectedType() {
    return 'array';
  }
}

class ObjectContaining extends AsymmetricMatcher<
  Record<string | symbol, unknown>
> {
  constructor(sample: Record<string | symbol, unknown>, inverse = false) {
    super(sample, inverse);
  }

  asymmetricMatch(other: any) {
    // Ensures that the argument passed to the objectContaining method is an object
    if (typeof this.sample !== 'object') {
      throw new TypeError(
        `You must provide an object to ${this.toString()}, not '${typeof this
          .sample}'.`,
      );
    }

    // Ensures that the argument passed to the expect function is an object
    // This is necessary to avoid matching of non-object values
    // Arrays are a special type of object, but having a valid match with a standard object
    // does not make sense, hence we do a simple array check
    if (typeof other !== 'object' || Array.isArray(other)) {
      return false;
    }

    let result = true;

    const matcherContext = this.getMatcherContext();
    const objectKeys = getObjectKeys(this.sample);

View on GitHub (pinned to 8e6d128e4a)