jestjs/jest · error · Error

${matcherHintFromConfig(...)} received value must be an obje

Error message

${matcherHintFromConfig(...)} received value must be an object when the matcher has properties
${printWithType('Received', received, printReceived)}

What it means

Thrown by `_toMatchSnapshot` (index.ts:319-332) when properties are supplied to the matcher but `received` is not a non-null object. Property-subset matching (`context.equals(received, properties, ...)`) only makes sense when both sides are objects; passing properties against a primitive is a usage error.

Source

Thrown at packages/jest-snapshot/src/index.ts:321

  if (snapshotState == null) {
    // Because the state is the problem, this is not a matcher error.
    // Call generic stringify from jest-matcher-utils package
    // because uninitialized snapshot state does not need snapshot serializers.
    throw new Error(
      `${matcherHintFromConfig(config, false)}\n\n` +
        'Snapshot state must be initialized' +
        `\n\n${printWithType('Snapshot state', snapshotState, stringify)}`,
    );
  }

  const fullTestName =
    currentTestName && hint
      ? `${currentTestName}: ${hint}`
      : currentTestName || ''; // future BREAKING change: || hint

  if (typeof properties === 'object') {
    if (typeof received !== 'object' || received === null) {
      throw new Error(
        matcherErrorMessage(
          matcherHintFromConfig(config, false),
          `${RECEIVED_COLOR(
            'received',
          )} value must be an object when the matcher has ${EXPECTED_COLOR(
            'properties',
          )}`,
          printWithType('Received', received, printReceived),
        ),
      );
    }

    const propertyPass = context.equals(received, properties, [
      context.utils.iterableEquality,
      context.utils.subsetEquality,
    ]);

    if (propertyPass) {

View on GitHub (pinned to f49721c78e)

Solutions

  1. Drop the properties argument when asserting on a primitive: `expect(42).toMatchSnapshot()`.
  2. Wrap the value in an object if you really want property matching: `expect({ value: 42 }).toMatchSnapshot({ value: expect.any(Number) })`.
  3. Assert the primitive directly with a string snapshot.

Example fix

// before
expect(statusCode).toMatchSnapshot({ min: 200 });

// after
expect({ statusCode }).toMatchSnapshot({ statusCode: expect.any(Number) });
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof received !== 'object' || received === null) {
  throw new Error('Received must be a non-null object when using properties');
}
expect(received).toMatchSnapshot(properties);

Type guard

function isNonNullObject(v: unknown): v is object {
  return typeof v === 'object' && v !== null;
}

Prevention

When it happens

Trigger: `expect(42).toMatchSnapshot({ min: 0 })`, `expect('hello').toMatchInlineSnapshot({ length: 5 })`, `expect(null).toMatchSnapshot({})`.

Common situations: Passing a primitive where an object was intended. Accidentally including a properties argument for a scalar assertion.

Related errors


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