{"record":{"id":"db4962f3c8630cfe","repo":"jestjs/jest","slug":"received-color-received-value-must-be-a-non","errorCode":null,"errorMessage":"${RECEIVED_COLOR('received')} value must be a non-null object","messagePattern":"(.+?) value must be a non-null object","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/matchers.ts","lineNumber":902,"sourceCode":"            matcherHint(matcherName, undefined, undefined, options) +\n            '\\n\\n' +\n            `${printLabel(labelExpected)}${printExpected(expected)}\\n` +\n            `${printLabel(labelReceived)}${printReceived(received)}`\n          );\n        };\n\n    return {message, pass};\n  },\n\n  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","sourceCodeStart":884,"sourceCodeEnd":920,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/matchers.ts#L884-L920","documentation":"Thrown by `toMatchObject` (matchers.ts:901) when the received value is not a non-null object. `toMatchObject` checks that the received object contains the expected subset of properties; it requires `typeof received === 'object' && received !== null`, so primitives, null, and undefined are rejected.","triggerScenarios":"Calling `expect(value).toMatchObject({a:1})` where `value` is a primitive (number, string, boolean), null, undefined, or a symbol.","commonSituations":"A function returns a primitive in an edge case (e.g. a number code) when an object was expected; the property accessed is a scalar not an object; an optional object is null/undefined after a schema change.","solutions":["Ensure received is an object: assert the source returns an object for the input.","If the value may be null/undefined, guard first: expect(value).toBeDefined().","For scalar comparisons use toEqual/toBe, not toMatchObject.","Default the root: expect(value ?? {}).toMatchObject({a:1}) only if an empty-object fallback is meaningful."],"exampleFix":"// before\nexpect(getSettings()).toMatchObject({theme: 'dark'}); // returns undefined\nexpect(status).toMatchObject({code: 200});            // status is a number\n\n// after\nexpect(getSettings() ?? {}).toMatchObject({theme: 'dark'});\nexpect(status).toEqual(200);","handlingStrategy":"type-guard","validationCode":"const v = received;\nif (typeof v !== 'object' || v === null) {\n  throw new Error('toMatchObject requires a non-null object received value');\n}\nexpect(v).toMatchObject({a: 1});","typeGuard":"const isNonNullObject = (v: unknown): v is object =>\n  v !== null && typeof v === 'object';","tryCatchPattern":null,"preventionTips":["Use toEqual/toBe for scalars, toMatchObject for object subsets.","Guard optional objects before subset matching.","Tighten return types so the value is always an object."],"tags":["jest","expect","matcher","type-error","tomatchobject","object"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}