{"record":{"id":"745e839289788581","repo":"jestjs/jest","slug":"expected-color-expected-value-must-be-a-func","errorCode":null,"errorMessage":"${EXPECTED_COLOR('expected')} value must be a function","messagePattern":"(.+?) value must be a function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/expect/src/matchers.ts","lineNumber":303,"sourceCode":"    const message = () =>\n      // eslint-disable-next-line prefer-template\n      matcherHint(matcherName, undefined, undefined, options) +\n      '\\n\\n' +\n      `Expected:${isNot ? ' not' : ''} >= ${printExpected(expected)}\\n` +\n      `Received:${isNot ? '    ' : ''}    ${printReceived(received)}`;\n\n    return {message, pass};\n  },\n\n  toBeInstanceOf(received: any, expected: Function) {\n    const matcherName = 'toBeInstanceOf';\n    const options: MatcherHintOptions = {\n      isNot: this.isNot,\n      promise: this.promise,\n    };\n\n    if (typeof expected !== 'function') {\n      throw new TypeError(\n        matcherErrorMessage(\n          matcherHint(matcherName, undefined, undefined, options),\n          `${EXPECTED_COLOR('expected')} value must be a function`,\n          printWithType('Expected', expected, printExpected),\n        ),\n      );\n    }\n\n    const pass = received instanceof expected;\n\n    const message = pass\n      ? () =>\n          // eslint-disable-next-line prefer-template\n          matcherHint(matcherName, undefined, undefined, options) +\n          '\\n\\n' +\n          printExpectedConstructorNameNot('Expected constructor', expected) +\n          (typeof received.constructor === 'function' &&\n          received.constructor !== expected","sourceCodeStart":285,"sourceCodeEnd":321,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/matchers.ts#L285-L321","documentation":"Thrown by `toBeInstanceOf` (matchers.ts:302) when the `expected` argument is not a function (i.e. not a constructor/class). `instanceof` requires a right-hand side that is a callable constructor, so the matcher validates `typeof expected === 'function'` before evaluating `received instanceof expected`.","triggerScenarios":"Calling `expect(x).toBeInstanceOf(SomeClass)` where SomeClass is undefined (e.g. a failed/typo'd import), null, a string class name like 'Error', an object, or an instance instead of the class itself.","commonSituations":"Named import typo or circular import causing the class binding to be undefined at test time; passing an instance (`new Error()`) instead of the constructor (`Error`); passing a string name from a config-driven test; mock module not returning a constructor.","solutions":["Pass the constructor/class itself, not an instance: toBeInstanceOf(Error), not toBeInstanceOf(new Error()).","Check the import resolves: confirm the class is exported and the import path/spelling is correct (a failed import gives undefined).","If testing by name, switch to toHaveProperty('constructor.name', 'Error') or a custom check instead of toBeInstanceOf.","For circular-import undefined bindings, move the import or restructure so the class is defined at assertion time."],"exampleFix":"// before\nimport {Er ror} from './errors'; // typo -> undefined\nexpect(err).toBeInstanceOf(Er ror);\n\n// after\nimport {MyError} from './errors';\nexpect(err).toBeInstanceOf(MyError);\n// pass the class, not an instance\nexpect(err).toBeInstanceOf(Error);","handlingStrategy":"type-guard","validationCode":"const ctor = ExpectedClass;\nif (typeof ctor !== 'function') {\n  throw new Error('toBeInstanceOf expected must be a constructor (function)');\n}\nexpect(received).toBeInstanceOf(ctor);","typeGuard":"const isConstructor = (v: unknown): v is new (...args: any[]) => any =>\n  typeof v === 'function';","tryCatchPattern":null,"preventionTips":["Verify imports resolve (a typo'd/circular import yields undefined).","Pass the class, never an instance.","For dynamic class references, guard typeof === 'function' first."],"tags":["jest","expect","matcher","type-error","tobeinstanceof","class","import"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}