{"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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/expect/src/jest-expect.ts#L209-L245","documentation":"TypeError thrown synchronously by the toMatch matcher when the actual value (this._obj) is not a string. toMatch only operates on strings (substring or RegExp match); passing any other primitive or object is a programming error, not an assertion failure.","triggerScenarios":"expect(123).toMatch('foo'); expect(undefined).toMatch(/x/); expect({a:1}).toMatch('a'); expect(null).toMatch('x'). Any call where the assertion target is not a string triggers the typeof actual !== 'string' guard at line 226-229.","commonSituations":"A variable that the developer believed was a string is actually a number, undefined, or an object — e.g. reading a JSON field that was parsed as a number, or accessing a property that does not exist (undefined). Also common after a refactor changes a function's return type.","solutions":["Coerce or assert the value is a string before matching: expect(String(value)).toMatch(/x/) or guard with typeof first.","Fix the upstream code so the value is actually a string.","If matching object shapes, use toMatchObject instead of toMatch."],"exampleFix":"// before\nexpect(response.statusCode).toMatch('2');\n// after\nexpect(String(response.statusCode)).toMatch(/^2\\d{2}$/);","handlingStrategy":"type-guard","validationCode":"if (typeof actual !== 'string') throw new TypeError(`toMatch target is ${typeof actual}, expected string`);","typeGuard":"const isString = (v: unknown): v is string => typeof v === 'string';","tryCatchPattern":null,"preventionTips":["Type the producer so the value is statically string.","Add a runtime typeof check at the boundary that produces the value."],"tags":["typeerror","tomatch","string","type-mismatch"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}