{"record":{"id":"e91b2f2612b12ebc","repo":"jestjs/jest","slug":"received-color-received-value-must-not-be-nu","errorCode":null,"errorMessage":"${RECEIVED_COLOR('received')} value must not be null nor undefined","messagePattern":"(.+?) value must not be null nor undefined","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/matchers.ts","lineNumber":475,"sourceCode":"      // eslint-disable-next-line prefer-template\n      matcherHint(matcherName, undefined, '', options) +\n      '\\n\\n' +\n      `Received: ${printReceived(received)}`;\n\n    return {message, pass};\n  },\n\n  toContain(received: ContainIterable | string, expected: unknown) {\n    const matcherName = 'toContain';\n    const isNot = this.isNot;\n    const options: MatcherHintOptions = {\n      comment: 'indexOf',\n      isNot,\n      promise: this.promise,\n    };\n\n    if (received == null) {\n      throw new Error(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, undefined, options),\n          `${RECEIVED_COLOR('received')} value must not be null nor undefined`,\n          printWithType('Received', received, printReceived),\n        ),\n      );\n    }\n\n    if (typeof received === 'string') {\n      const wrongTypeErrorMessage = `${EXPECTED_COLOR(\n        'expected',\n      )} value must be a string if ${RECEIVED_COLOR(\n        'received',\n      )} value is a string`;\n\n      if (typeof expected !== 'string') {\n        throw new TypeError(\n          matcherErrorMessage(","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/matchers.ts#L457-L493","documentation":"Thrown by `toContain` (matchers.ts:474) when the received value passed to `expect(...)` is null or undefined. `toContain` works on strings and indexable iterables (arrays, sets, NodeLists); it must spread/index the value (`[...received]`), which fails on null/undefined. The guard uses loose equality (`received == null`) to catch both.","triggerScenarios":"Calling `expect(value).toContain(item)` where `value` is null or undefined. Typically the value comes from a property access, an array method (find returning undefined), an optional that was never set, or an API call that returned null for a missing resource.","commonSituations":"Querying the DOM (.querySelector) returned null; an Array.find returned undefined and you then assert on it; a JSON field absent in the response; a spy return value not captured; refactoring changed a function to possibly return undefined.","solutions":["Guard the value is non-null before the matcher, or assert it first: expect(value).toBeDefined().","Fix the source so it returns an empty array/string instead of null/undefined for 'no results'.","Use optional chaining when extracting: const value = obj?.items ?? [].","If null is legitimately possible, branch the test to assert the null case separately instead of calling toContain."],"exampleFix":"// before\nexpect(findUser(id)?.roles).toContain('admin'); // findUser returns null\n\n// after\nconst user = findUser(id);\nexpect(user).toBeDefined();\nexpect(user.roles).toContain('admin');\n// or default to empty\nexpect(user?.roles ?? []).toContain('admin');","handlingStrategy":"validation","validationCode":"const value = collection;\nif (value == null) {\n  throw new Error('cannot call toContain on null/undefined; check the source');\n}\nexpect(value).toContain(item);","typeGuard":"const isIndexable = (v: unknown): v is string | Array<unknown> | Set<unknown> =>\n  v != null && (typeof v === 'string' || Array.isArray(v) || v instanceof Set || Symbol.iterator in Object(v));","tryCatchPattern":null,"preventionTips":["Default collections to empty arrays/strings at the source instead of null.","Assert defined-ness before containment matchers.","Use optional chaining with ?? [] when extracting optional fields."],"tags":["jest","expect","matcher","type-error","tocontain","null-check"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}