{"record":{"id":"f6809a26e6814876","repo":"jestjs/jest","slug":"expected-color-expected-value-must-be-a-stri-f6809a","errorCode":null,"errorMessage":"${EXPECTED_COLOR('expected')} value must be a string or regular expression or class or error","messagePattern":"(.+?) value must be a string or regular expression or class or error","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/toThrowMatchers.ts","lineNumber":129,"sourceCode":"    }\n\n    if (expected === undefined) {\n      return toThrow(matcherName, options, thrown);\n    } else if (typeof expected === 'function') {\n      return toThrowExpectedClass(matcherName, options, thrown, expected);\n    } else if (typeof expected === 'string') {\n      return toThrowExpectedString(matcherName, options, thrown, expected);\n    } else if (expected !== null && typeof expected.test === 'function') {\n      return toThrowExpectedRegExp(matcherName, options, thrown, expected);\n    } else if (\n      expected !== null &&\n      typeof expected.asymmetricMatch === 'function'\n    ) {\n      return toThrowExpectedAsymmetric(matcherName, options, thrown, expected);\n    } else if (expected !== null && typeof expected === 'object') {\n      return toThrowExpectedObject(matcherName, options, thrown, expected);\n    } else {\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 or class or error`,\n          printWithType('Expected', expected, printExpected),\n        ),\n      );\n    }\n  };\n\nconst matchers: MatchersObject = {\n  toThrow: createMatcher('toThrow'),\n};\n\nconst toThrowExpectedRegExp = (\n  matcherName: string,\n  options: MatcherHintOptions,","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/toThrowMatchers.ts#L111-L147","documentation":"Thrown by the `toThrow` matcher factory (toThrowMatchers.ts:129) when the `expected` argument matches none of the supported forms: string (substring), function/class (constructor), RegExp-like (.test), asymmetric matcher (.asymmetricMatch), or error object. If expected is something else (number, boolean, null, plain non-Error object without the right shape), the matcher cannot decide how to match the thrown value and aborts.","triggerScenarios":"Calling `expect(fn).toThrow(404)` (number), `toThrow(true)` (boolean), `toThrow(null)`, `toThrow({code: 1})` with a plain object that is not an Error, or `toThrow(['msg'])` (array).","commonSituations":"Passing an error code/number instead of a class or message; passing a plain object to match a thrown non-Error; intending an asymmetric matcher but passing a raw value; a variable resolving to null.","solutions":["Pass a substring message, a RegExp, an Error class, or an Error instance: toThrow('not found'), toThrow(NotFound), toThrow(/not found/), toThrow(new Error('x')).","For numeric/string codes on custom errors, assert on the thrown instance via a class: toThrow(MyError) and then check .code separately.","Use an asymmetric matcher for flexible matching: toThrow({message: expect.stringContaining('x')}) (note: object path expects an Error-like shape).","If expected may be null/undefined from a lookup, default it or branch the test."],"exampleFix":"// before\nexpect(fn).toThrow(404);\nexpect(fn).toThrow({code: 'ERR'});\n\n// after\nexpect(fn).toThrow('not found');\nexpect(fn).toThrow(NotFoundError);\nexpect(fn).toThrow(/not found/);\nexpect(fn).toThrow(new NotFoundError());","handlingStrategy":"type-guard","validationCode":"const expected = matcher;\nconst ok =\n  expected === undefined ||\n  typeof expected === 'string' ||\n  typeof expected === 'function' ||\n  (expected instanceof RegExp) ||\n  (expected != null && typeof (expected as any).test === 'function') ||\n  (expected != null && typeof (expected as any).asymmetricMatch === 'function') ||\n  (expected != null && typeof expected === 'object');\nif (!ok) {\n  throw new Error('toThrow expected must be string | RegExp | class | Error | asymmetric matcher');\n}\nexpect(fn).toThrow(expected);","typeGuard":"type ToThrowExpected =\n  | string\n  | RegExp\n  | (new (...args: any[]) => Error)\n  | Error\n  | { asymmetricMatch: (v: unknown) => boolean };\nconst isToThrowExpected = (v: unknown): v is ToThrowExpected =>\n  v == null ? false :\n  typeof v === 'string' || typeof v === 'function' ||\n  typeof (v as any).test === 'function' ||\n  typeof (v as any).asymmetricMatch === 'function' ||\n  (typeof v === 'object' && v instanceof Error);","tryCatchPattern":null,"preventionTips":["Pass a substring, RegExp, Error class, or Error instance to toThrow.","For numeric codes, assert on a custom Error subclass instead of a bare number.","Keep expected as a defined value; default or branch when lookups may be null."],"tags":["jest","expect","matcher","type-error","tothrow","error"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}