{"id":"3214870845a46d6d","repo":"jestjs/jest","slug":"received-value-must-be-a-string","errorCode":null,"errorMessage":"received value must be a string","messagePattern":"received value must be a string","errorType":"validation","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/f49721c78e195558b40913977c9230f5b7f559d8/packages/expect/src/matchers.ts#L806-L842","documentation":"Thrown by `toMatch` (matchers.ts:816) when the value passed to `expect(...)` is not a string. `toMatch` runs `received.includes(expected)` or `new RegExp(expected).test(received)`, both of which require a string received; a non-string would either crash or silently misbehave. Jest throws a `TypeError` with a `printWithType` hint before any matching runs.","triggerScenarios":"Calling `expect(value).toMatch(/pattern/)` where `value` is a number, object, undefined, null, or an array. Common with `expect(statusCode).toMatch(200)` (use `toBe`), `expect(response.body).toMatch(/ok/)` where body is an object (stringify first), or `expect(Date.now()).toMatch(...)`.","commonSituations":"Confusing `toMatch` (string/regex) with equality matchers; asserting on a non-serialized object; receiving a Buffer or Uint8Array instead of a string; refactoring that changed a return type from string to object.","solutions":["If checking equality on a non-string, switch to `toBe` or `toEqual`.","If matching against serialized output, stringify first: `expect(JSON.stringify(obj)).toMatch(/pattern/)`.","For Buffers/typed arrays, convert: `expect(buf.toString('utf8')).toMatch(...)`.","Log `typeof received` to confirm it is a string before the assertion."],"exampleFix":"// before\nexpect(response).toMatch(/success/i); // response is an object\n\n// after\nexpect(JSON.stringify(response)).toMatch(/success/i);","handlingStrategy":"type-guard","validationCode":"if (typeof received !== 'string') {\n  throw new Error(`received must be a string, got ${typeof received}`);\n}\nexpect(received).toMatch(pattern);","typeGuard":"const isString = (v: unknown): v is string => typeof v === 'string';","tryCatchPattern":"try {\n  expect(received).toMatch(pattern);\n} catch (e) {\n  if (e instanceof TypeError && /received value must be a string/.test(e.message)) {\n    console.error('received was', typeof received, received);\n  }\n  throw e;\n}","preventionTips":["Use `toBe`/`toEqual` for non-string equality, not `toMatch`.","Stringify objects before pattern matching: `JSON.stringify(obj)`.","Convert Buffers with `.toString('utf8')` before asserting."],"tags":["jest","expect","type-validation","assertion","string","regex"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}