{"record":{"id":"0a8a9282c76999c5","repo":"vitest-dev/vitest","slug":"tomatch-expects-to-receive-a-string-but-got","errorCode":null,"errorMessage":".toMatch() expects to receive a string, but got ${typeof actual}","messagePattern":"\\.toMatch\\(\\) expects to receive a string, but got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/expect/src/jest-expect.ts","lineNumber":227,"sourceCode":"        false,\n      ])\n      const message\n        = stripped === 0\n          ? msg\n          : `${msg}\\n(${stripped} matching ${\n            stripped === 1 ? 'property' : 'properties'\n          } omitted from actual)`\n      throw new AssertionError(message, {\n        showDiff: true,\n        expected,\n        actual: actualSubset,\n      })\n    }\n  })\n  def('toMatch', function (expected: string | RegExp) {\n    const actual = this._obj as string\n    if (typeof actual !== 'string') {\n      throw new TypeError(\n        `.toMatch() expects to receive a string, but got ${typeof actual}`,\n      )\n    }\n\n    return this.assert(\n      typeof expected === 'string'\n        ? actual.includes(expected)\n        : actual.match(expected),\n      `expected #{this} to match #{exp}`,\n      `expected #{this} not to match #{exp}`,\n      expected,\n      actual,\n    )\n  })\n  def('toContain', function (item) {\n    const actual = this._obj as\n      | Iterable<unknown>\n      | string","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/expect/src/jest-expect.ts#L209-L245","documentation":"The toMatch matcher requires the actual value (this._obj) to be a string; it then does either includes (for a string expected) or match (for a RegExp expected). A non-string actual throws a TypeError before any matching.","triggerScenarios":"expect(actual).toMatch(expected) where actual is a number, object, array, null, undefined, etc.","commonSituations":"Calling toMatch on a number expecting it to match a numeric pattern (use toBeCloseTo / type coercion); calling toMatch on a parsed JSON object instead of a string field; calling on undefined from a missing property.","solutions":["Ensure actual is a string: extract the right field (e.g. res.body.message) or coerce with String(actual).","Use the correct matcher for non-strings (toBe, toEqual, toBeCloseTo, toContain).","Add a typeof guard or non-null check before the assertion."],"exampleFix":"// before\nexpect(statusCode).toMatch(200)\n// after\nexpect(String(statusCode)).toMatch(/^2\\d\\d$/)\n// or, more idiomatic\nexpect(statusCode).toBe(200)","handlingStrategy":"type-guard","validationCode":"function assertStringForMatch(actual) {\n  if (typeof actual !== 'string') {\n    throw new TypeError(`.toMatch() requires a string, got ${typeof actual}`)\n  }\n}","typeGuard":"function isString(v): v is string {\n  return typeof v === 'string'\n}","tryCatchPattern":null,"preventionTips":["Confirm the actual value is a string field before calling toMatch.","Coerce with String(actual) only when the conversion is meaningful.","Pick the matcher that matches the value type (toBe, toEqual, toContain)."],"tags":["expect","matcher","type-error"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}