{"id":"2afb466226b0729b","repo":"jestjs/jest","slug":"expected-value-must-be-a-function","errorCode":null,"errorMessage":"expected value must be a function","messagePattern":"expected value must be a function","errorType":"validation","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/f49721c78e195558b40913977c9230f5b7f559d8/packages/expect/src/matchers.ts#L285-L321","documentation":"Thrown by `toBeInstanceOf` (matchers.ts:295) when the `expected` argument is not a function (i.e. not a constructor/class). `received instanceof expected` requires the right-hand side to be a callable constructor; anything else is a programmer error, not a test outcome. Jest throws a `TypeError` with `printWithType('Expected', expected, ...)` to surface what was passed.","triggerScenarios":"Calling `expect(x).toBeInstanceOf('Error')` (string instead of class), `expect(x).toBeInstanceOf({name: 'Error'})` (object literal), `expect(x).toBeInstanceOf(errorInstance)` (an instance instead of the class), or `expect(x).toBeInstanceOf(undefined)` when the imported class failed to load (e.g. named export mismatch).","commonSituations":"Importing a class under the wrong name (`import {Errors} from './errors'` when the export is `Error`); passing an instance rather than a constructor; mocking a class export so the import becomes `undefined`; copy-pasting a string error name from documentation.","solutions":["Pass the constructor/class itself, not an instance or its name: `.toBeInstanceOf(Error)`.","Verify the import resolves to a real class — log `typeof Expected` (should be `'function'`).","If the class is dynamically mocked, ensure the mock returns the constructor, not an instance.","If you only have a class name string, use `.toThrow()` with a string or match the constructor name manually."],"exampleFix":"// before\nimport { MyError } from './errors'; // MyError is actually an instance or undefined\nexpect(err).toBeInstanceOf(MyError);\n\n// after\nimport { MyErrorClass } from './errors';\nexpect(err).toBeInstanceOf(MyErrorClass);","handlingStrategy":"type-guard","validationCode":"if (typeof ExpectedClass !== 'function') {\n  throw new Error(`Expected must be a class/function, got ${typeof ExpectedClass}`);\n}\nexpect(value).toBeInstanceOf(ExpectedClass);","typeGuard":"const isConstructor = (v: unknown): v is new (...args: any[]) => any =>\n  typeof v === 'function';","tryCatchPattern":"try {\n  expect(value).toBeInstanceOf(ExpectedClass);\n} catch (e) {\n  if (e instanceof TypeError && /must be a function/.test(e.message)) {\n    console.error('ExpectedClass was', ExpectedClass);\n  }\n  throw e;\n}","preventionTips":["Import classes by their exact exported name; verify with `typeof` after import.","Pass the class itself, never an instance or a string name.","If mocking a class export, ensure the mock preserves the constructor shape."],"tags":["jest","expect","type-validation","assertion","classes","imports"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}