{"record":{"id":"82e64674b1423858","repo":"jestjs/jest","slug":"expected-color-expected-value-must-be-a-stri-82e646","errorCode":null,"errorMessage":"${EXPECTED_COLOR('expected')} value must be a string or regular expression","messagePattern":"(.+?) value must be a string or regular expression","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/matchers.ts","lineNumber":837,"sourceCode":"      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`,\n          printWithType('Expected', expected, printExpected),\n        ),\n      );\n    }\n\n    const pass =\n      typeof expected === 'string'\n        ? received.includes(expected)\n        : new RegExp(expected).test(received);\n\n    const message = pass\n      ? () =>\n          typeof expected === 'string'","sourceCodeStart":819,"sourceCodeEnd":855,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/matchers.ts#L819-L855","documentation":"Thrown by `toMatch` (matchers.ts:836) when the expected value is neither a string nor a regex-like object (something with a `.test` function). The matcher branches on string (substring) vs RegExp (pattern); anything else (number, object without test, null, undefined, array) is rejected.","triggerScenarios":"Calling `expect('hello').toMatch(5)` (number), `expect('hello').toMatch({pattern: 'h'})` (plain object), `expect('hello').toMatch(null)`, or omitting the expected so it is undefined.","commonSituations":"Passing a glob or pattern string from a config and forgetting it must be a RegExp for pattern semantics; passing an object by mistake; a variable that is undefined because a lookup failed.","solutions":["Pass a string substring or a RegExp: toMatch('hel') or toMatch(/^hel/).","Convert a config pattern string into a RegExp when pattern semantics are needed: new RegExp(pattern).","Ensure the expected variable is defined (a failed lookup gives undefined).","Do not pass plain objects or numbers; use the correct matcher for those types."],"exampleFix":"// before\nexpect(text).toMatch('*error*'); // treated as literal substring\nexpect(text).toMatch(404);\n\n// after\nexpect(text).toMatch('error');\nexpect(text).toMatch(/error/);\n// convert a glob-ish config\nexpect(text).toMatch(new RegExp(configPattern));","handlingStrategy":"type-guard","validationCode":"const expected = pattern;\nif (typeof expected !== 'string' && !(expected instanceof RegExp)) {\n  throw new Error('toMatch expected must be a string or RegExp');\n}\nexpect(text).toMatch(expected);","typeGuard":"const isStringOrRegExp = (v: unknown): v is string | RegExp =>\n  typeof v === 'string' || v instanceof RegExp;","tryCatchPattern":null,"preventionTips":["Wrap config pattern strings in new RegExp() when you need regex semantics.","Keep substring (string) vs pattern (RegExp) intentions explicit.","Ensure the expected variable is defined."],"tags":["jest","expect","matcher","type-error","tomatch","regexp"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}