{"record":{"id":"d53ec3b479218886","repo":"jestjs/jest","slug":"received-color-received-value-must-be-a-func","errorCode":null,"errorMessage":"${RECEIVED_COLOR('received')} value must be a function","messagePattern":"(.+?) value must be a function","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/expect/src/toThrowMatchers.ts","lineNumber":102,"sourceCode":"      isNot: this.isNot,\n      promise: this.promise,\n    };\n\n    let thrown = null;\n\n    if (fromPromise && isError(received)) {\n      thrown = getThrown(received);\n    } else {\n      if (typeof received === 'function') {\n        try {\n          received();\n        } catch (error) {\n          thrown = getThrown(error);\n        }\n      } else {\n        if (!fromPromise) {\n          const placeholder = expected === undefined ? '' : 'expected';\n          throw new Error(\n            matcherErrorMessage(\n              matcherHint(matcherName, undefined, placeholder, options),\n              `${RECEIVED_COLOR('received')} value must be a function`,\n              printWithType('Received', received, printReceived),\n            ),\n          );\n        }\n      }\n    }\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);","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/toThrowMatchers.ts#L84-L120","documentation":"Thrown by the `toThrow` matcher factory (toThrowMatchers.ts:102) when, in the synchronous (non-promise) path, the received value is not a function. `toThrow` invokes `received()` inside a try/catch to capture a thrown error, so it requires a callable. The guard is skipped only when `fromPromise` is true (i.e. `.rejects.toThrow()`), where received is already the rejection reason.","triggerScenarios":"Calling `expect(value).toThrow()` where `value` is a number, string, object, null, undefined, or the result of calling a function (instead of the function itself). The most common mistake is invoking the function (`expect(fn()).toThrow()`) so received is the return value, not the function.","commonSituations":"Passing `fn()` instead of `fn` (eager invocation) — the error throws before expect can catch it, or a non-error return reaches the matcher; passing a value you expected to be a function; forgetting to use `.rejects` for async rejections (then a Promise is received synchronously).","solutions":["Pass the function reference, not its result: expect(fn).toThrow(), not expect(fn()).toThrow().","For async rejections, use await expect(promise).rejects.toThrow() so the promise path is taken.","If the value is genuinely not a function, use a different matcher (toBeInstanceOf, toEqual) for it.","Wrap an inline throw in an arrow function: expect(() => { throw new Error() }).toThrow()."],"exampleFix":"// before\nexpect(throwIfBad(input)).toThrow();  // invokes eagerly, no fn\nexpect(badValue).toThrow();            // badValue is not callable\n\n// after\nexpect(() => throwIfBad(input)).toThrow();\nexpect(throwIfBad).toThrow();\n// async\nawait expect(promise).rejects.toThrow();","handlingStrategy":"type-guard","validationCode":"const v = received;\nif (typeof v !== 'function') {\n  throw new Error('toThrow requires a function reference, not its result');\n}\nexpect(v).toThrow();\n// for async: await expect(promise).rejects.toThrow();","typeGuard":"const isFunction = (v: unknown): v is Function =>\n  typeof v === 'function';","tryCatchPattern":"// wrap inline behaviour in an arrow fn\nexpect(() => {\n  throwIfBad(input);\n}).toThrow();","preventionTips":["Pass the function reference, not fn().","Use .rejects.toThrow() for promise rejections.","Wrap inline throws in () => { ... } for toThrow."],"tags":["jest","expect","matcher","type-error","tothrow","function"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}