{"record":{"id":"6330ece613837ae1","repo":"jestjs/jest","slug":"received-color-received-value-must-be-a-stri","errorCode":null,"errorMessage":"${RECEIVED_COLOR('received')} value must be a string","messagePattern":"(.+?) value must be a string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/expect/src/matchers.ts","lineNumber":824,"sourceCode":"                  : receivedPath.join('.'),\n              )}\\n\\n${\n                hasValue\n                  ? `Expected value: ${printExpected(expectedValue)}\\n`\n                  : ''\n              }Received value: ${printReceived(receivedValue)}`);\n\n    return {message, pass};\n  },\n\n  toMatch(received: string, expected: string | RegExp) {\n    const matcherName = 'toMatch';\n    const options: MatcherHintOptions = {\n      isNot: this.isNot,\n      promise: this.promise,\n    };\n\n    if (typeof received !== 'string') {\n      throw new TypeError(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, undefined, options),\n          `${RECEIVED_COLOR('received')} value must be a string`,\n          printWithType('Received', received, printReceived),\n        ),\n      );\n    }\n\n    if (\n      !(typeof expected === 'string') &&\n      !(expected && typeof expected.test === 'function')\n    ) {\n      throw new Error(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, undefined, options),\n          `${EXPECTED_COLOR(\n            'expected',\n          )} value must be a string or regular expression`,","sourceCodeStart":806,"sourceCodeEnd":842,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/matchers.ts#L806-L842","documentation":"Thrown by `toMatch` (matchers.ts:823) when the received value is not a string. `toMatch` checks substring or regex membership against a string; it explicitly requires `typeof received === 'string'` before testing, refusing to coerce.","triggerScenarios":"Calling `expect(value).toMatch(/pattern/)` or `expect(value).toMatch('sub')` where `value` is a number, object, null, undefined, array, or boolean.","commonSituations":"Value is undefined because a function returned early; value is a number that the developer assumed would be stringified; the property accessed is actually an object; a Date object passed instead of its ISO string.","solutions":["Ensure received is a string: stringify numbers, call .toISOString() on dates, .toString() where meaningful.","If testing object/array shape, use toEqual/toMatchObject instead of toMatch.","If the value may be undefined, guard or default it: expect(String(value ?? '')).toMatch(...).","Fix the source to return the string field you actually intended to match."],"exampleFix":"// before\nexpect(response.code).toMatch(/^ERR/); // response.code is a number\nexpect(event.timestamp).toMatch(/2024/);  // Date object\n\n// after\nexpect(String(response.code)).toMatch(/^ERR/);\nexpect(event.timestamp.toISOString()).toMatch(/2024/);","handlingStrategy":"type-guard","validationCode":"const v = received;\nif (typeof v !== 'string') {\n  throw new Error('toMatch requires a string received value');\n}\nexpect(v).toMatch(/pattern/);","typeGuard":"const isString = (v: unknown): v is string => typeof v === 'string';","tryCatchPattern":null,"preventionTips":["Stringify non-strings explicitly before toMatch.","Call .toISOString() on dates, String() on numbers.","Use toEqual/toMatchObject for non-string shapes."],"tags":["jest","expect","matcher","type-error","tomatch","string"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}