{"record":{"id":"7474b6ff812f8688","repo":"awslabs/llrt","slug":"tothrow-expects-string-regexp-function-error-instance-or","errorCode":null,"errorMessage":"\"toThrow\" expects string, RegExp, function, Error instance or asymmetric matcher, got \"${typeof expected}\"","messagePattern":"\"toThrow\" expects string, RegExp, function, Error instance or asymmetric matcher, got \"(.+?)\"","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"llrt_core/src/modules/js/@llrt/expect/jest-expect.ts","lineNumber":508,"sourceCode":"        );\n      }\n\n      if (\n        typeof expected === \"object\" &&\n        \"asymmetricMatch\" in expected &&\n        typeof (expected as any).asymmetricMatch === \"function\"\n      ) {\n        const matcher = expected as any as AsymmetricMatcher<any>;\n        return this.assert(\n          thrown && matcher.asymmetricMatch(thrown),\n          \"expected error to match asymmetric matcher\",\n          \"expected error not to match asymmetric matcher\",\n          matcher,\n          thrown\n        );\n      }\n\n      throw new Error(\n        `\"toThrow\" expects string, RegExp, function, Error instance or asymmetric matcher, got \"${typeof expected}\"`\n      );\n    }\n  );\n\n  def(\"toSatisfy\", function (matcher: Function, message?: string) {\n    return this.be.satisfy(matcher, message);\n  });\n\n  utils.addProperty(\n    chai.Assertion.prototype,\n    \"resolves\",\n    function __VITEST_RESOLVES__(this: any) {\n      const error = new Error(\"resolves\");\n      utils.flag(this, \"promise\", \"resolves\");\n      utils.flag(this, \"error\", error);\n      const test: any = utils.flag(this, \"vitest-test\");\n      const obj = utils.flag(this, \"object\");","sourceCodeStart":490,"sourceCodeEnd":526,"githubUrl":"https://github.com/awslabs/llrt/blob/742fc00b82cbeaab1c1b76f0d706c302a5cbc306/llrt_core/src/modules/js/@llrt/expect/jest-expect.ts#L490-L526","documentation":"This Error is thrown by JestChaiExpect's `toThrow` matcher in @llrt/expect when the expected value passed to `.toThrow()` is none of the supported types: string, RegExp, function, Error instance, or asymmetric matcher. The library validates the argument type after exhausting all matcher branches (string equality, regex match, class/constructor, error instance, asymmetric matcher) and panics with the runtime `typeof` of the bad value. It exists to give a clear message instead of silently treating an unsupported value as a passing expectation.","triggerScenarios":"Calling expect(fn).toThrow(value) where value is e.g. an object, number, array, boolean, null, or undefined — any value whose typeof is not string/function/object-with-Error-shape and which fails the asymmetric-matcher check.","commonSituations":"Passing an error message built by interpolation that ended up as a non-string, passing an error's `.message` extraction that returned undefined, or copying Jest examples that pass an object literal (Jest accepts Error-like objects; this build only accepts true Error instances).","solutions":["Pass an Error instance: expect(fn).toThrow(new Error('msg')) instead of an error-shaped plain object.","Pass the message as a string: expect(fn).toThrow('msg') or a RegExp: expect(fn).toThrow(/msg/).","Pass the error class itself: expect(fn).toThrow(MyError).","Log typeof expected before the call to confirm what is actually being passed (often undefined)."],"exampleFix":"// before\nexpect(fn).toThrow({ message: 'boom' }); // object is not supported\n// after\nexpect(fn).toThrow(new Error('boom')); // or: expect(fn).toThrow('boom');","handlingStrategy":"validation","validationCode":"const t = typeof expected;\nconst ok = t === 'string' || t === 'function' || t === 'undefined' || expected instanceof Error || (expected && typeof expected.asymmetricMatch === 'function');\nif (!ok) throw new TypeError('toThrow expects string, RegExp, function, Error or asymmetric matcher, got ' + t);","typeGuard":"function isToThrowArg(v) {\n  return typeof v === 'string' || typeof v === 'function' || v instanceof Error || (v !== null && typeof v === 'object' && typeof v.asymmetricMatch === 'function');\n}","tryCatchPattern":null,"preventionTips":["Never pass plain error-shaped objects to toThrow; wrap in new Error(...).","Assert on message strings or RegExp instead of error objects when possible.","Check that interpolated expected messages are actually strings (String(msg))."],"tags":["expect","argument-type","testing"],"backgroundTag":"invalid-argument-value","analyzedSha":"742fc00b82cbeaab1c1b76f0d706c302a5cbc306","analyzedAt":"2026-09-12T11:14:07.838Z","contentChangedAt":"2026-09-12T11:14:07.838Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}