{"id":"5eb1279b31a107c9","repo":"jestjs/jest","slug":"expected-value-must-be-a-string-or-regular-express","errorCode":null,"errorMessage":"expected value must be a string or regular expression","messagePattern":"expected value must be a string or regular expression","errorType":"validation","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/f49721c78e195558b40913977c9230f5b7f559d8/packages/expect/src/matchers.ts#L819-L855","documentation":"Thrown by `toMatch` (matchers.ts:816) when the expected argument is neither a string nor a regex-like object (an object with a `.test` function). `toMatch` accepts `string` (substring) or `RegExp` (pattern); anything else cannot be applied to a string. The check is duck-typed: it allows objects implementing `.test`, but rejects numbers, booleans, plain objects, null.","triggerScenarios":"Calling `expect(str).toMatch(123)` (number), `.toMatch(true)`, `.toMatch({pattern: 'x'})` (plain object), `.toMatch(null)`, or `.toMatch(['a','b'])` (array). Also `.toMatch(new String('x'))` would fail because a String object's `typeof` is `'object'` without a `.test`.","commonSituations":"Passing a token or ID number where a substring was intended; passing a config object that holds a regex instead of the regex itself; refactor that changed the expected from string to number; mis-importing a regex constant that resolved to undefined.","solutions":["Wrap the value as a regex if you want pattern matching: `.toMatch(new RegExp(value))`.","Coerce to string for substring matching: `.toMatch(String(value))`.","Verify the imported regex constant is actually a RegExp — log `expected instanceof RegExp`.","Use `toEqual` if you meant equality, not pattern matching."],"exampleFix":"// before\nexpect(msg).toMatch(patternConfig); // patternConfig is {regex: '/x/'}\n\n// after\nexpect(msg).toMatch(patternConfig.regex);","handlingStrategy":"type-guard","validationCode":"if (typeof expected !== 'string' && !(expected instanceof RegExp)) {\n  throw new Error(`expected must be string or RegExp, got ${typeof expected}`);\n}\nexpect(str).toMatch(expected);","typeGuard":"const isStringOrRegExp = (v: unknown): v is string | RegExp =>\n  typeof v === 'string' || v instanceof RegExp;","tryCatchPattern":"try {\n  expect(str).toMatch(expected);\n} catch (e) {\n  if (e instanceof Error && /must be a string or regular expression/.test(e.message)) {\n    console.error('expected was', typeof expected, expected);\n  }\n  throw e;\n}","preventionTips":["Import regex constants and verify `instanceof RegExp`.","Coerce values with `String(...)` or wrap with `new RegExp(...)`.","Use `toEqual` for equality instead of pattern matching."],"tags":["jest","expect","type-validation","assertion","string","regex"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}