{"id":"ed4622e7dabb5ca1","repo":"jestjs/jest","slug":"expected-value-must-be-a-non-null-object","errorCode":null,"errorMessage":"expected value must be a non-null object","messagePattern":"expected value must be a non-null object","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/matchers.ts","lineNumber":912,"sourceCode":"  toMatchObject(received: object, expected: object) {\n    const matcherName = 'toMatchObject';\n    const options: MatcherHintOptions = {\n      isNot: this.isNot,\n      promise: this.promise,\n    };\n\n    if (typeof received !== 'object' || received === null) {\n      throw new Error(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, undefined, options),\n          `${RECEIVED_COLOR('received')} value must be a non-null object`,\n          printWithType('Received', received, printReceived),\n        ),\n      );\n    }\n\n    if (typeof expected !== 'object' || expected === null) {\n      throw new Error(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, undefined, options),\n          `${EXPECTED_COLOR('expected')} value must be a non-null object`,\n          printWithType('Expected', expected, printExpected),\n        ),\n      );\n    }\n\n    const pass = equals(received, expected, [\n      ...this.customTesters,\n      iterableEquality,\n      subsetEquality,\n    ]);\n\n    const message = pass\n      ? () =>\n          // eslint-disable-next-line prefer-template\n          matcherHint(matcherName, undefined, undefined, options) +","sourceCodeStart":894,"sourceCodeEnd":930,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/expect/src/matchers.ts#L894-L930","documentation":"Thrown by `toMatchObject` (matchers.ts:894) when the expected argument (the subset template) is not a non-null object. `toMatchObject` walks the expected object's keys to verify they exist as a subset of received; a primitive/null/undefined expected cannot be iterated. The check is `typeof expected !== 'object' || expected === null`.","triggerScenarios":"Calling `expect(obj).toMatchObject('foo')` (string), `.toMatchObject(123)` (number), `.toMatchObject(null)`, `.toMatchObject(undefined)` (when an optional value was not supplied), or `.toMatchObject([1,2,3])` (array — technically allowed by the check but unusual; arrays pass since `typeof [] === 'object'`).","commonSituations":"Passing a primitive constant where a shape object was intended; refactor that replaced an object literal with a single value; importing a fixture that resolved to undefined; passing a class instance where a plain object was expected (instance is fine — it's an object).","solutions":["Pass a plain object describing the expected subset: `.toMatchObject({status: 'ok'})`.","If the expected is dynamic, validate it is a non-null object before the test.","Log `typeof expected` and confirm it is `'object'` and not `null`.","Use `toEqual` for full equality or `toContainEqual` for membership if a subset doesn't fit."],"exampleFix":"// before\nexpect(user).toMatchObject(user.status); // user.status is a string\n\n// after\nexpect(user).toMatchObject({status: user.status});","handlingStrategy":"type-guard","validationCode":"if (typeof expected !== 'object' || expected === null) {\n  throw new Error(`expected must be a non-null object, got ${expected === null ? 'null' : typeof expected}`);\n}\nexpect(received).toMatchObject(expected);","typeGuard":"const isNonNullObject = (v: unknown): v is Record<string, unknown> =>\n  typeof v === 'object' && v !== null;","tryCatchPattern":"try {\n  expect(received).toMatchObject(expected);\n} catch (e) {\n  if (e instanceof Error && /expected value must be a non-null object/.test(e.message)) {\n    console.error('expected was', typeof expected, expected);\n  }\n  throw e;\n}","preventionTips":["Pass a plain object literal as the subset template.","Validate imported fixtures are objects before use.","Avoid passing primitives or null where a shape object is intended."],"tags":["jest","expect","type-validation","assertion","object"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}