jestjs/jest · error · TypeError

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

Error message

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

What it means

ObjectContaining.asymmetricMatch requires typeof this.sample === 'object'; if a primitive (string/number/boolean/function/undefined/symbol/bigint) was passed to expect.objectContaining it throws TypeError. The matcher checks at match time (constructor only stores the sample). Note typeof null === 'object' so null slips past this guard but will then iterate over no keys.

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

Solutions

  1. Pass a plain object describing the subset of keys/values: expect.objectContaining({ id: 5 }).
  2. Parse JSON first if you have a string: expect.objectContaining(JSON.parse(str)).
  3. Use the correct matcher for primitives: expect.stringContaining, expect.arrayContaining, etc.

Example fix

// before
expect(received).toEqual(expect.objectContaining('{"id":5}'));

// after
expect(received).toEqual(expect.objectContaining({ id: 5 }));
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

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

Try / catch

// throws at match time; guard the sample before constructing the matcher

Prevention

When it happens

Trigger: Calling expect.objectContaining('foo'), expect.objectContaining(42), expect.objectContaining(undefined), or expect.objectContaining(() => {}) and then evaluating that matcher in a comparison.

Common situations: Confusing objectContaining with stringMatching/arrayContaining; passing a class instead of an instance descriptor; passing a JSON string instead of a parsed object.

Related errors


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