jestjs/jest · error · TypeError

${EXPECTED_COLOR('expected')} value must be a function

Error message

${EXPECTED_COLOR('expected')} value must be a function

What it means

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`.

Source

Thrown at packages/expect/src/matchers.ts:303

    const message = () =>
      // eslint-disable-next-line prefer-template
      matcherHint(matcherName, undefined, undefined, options) +
      '\n\n' +
      `Expected:${isNot ? ' not' : ''} >= ${printExpected(expected)}\n` +
      `Received:${isNot ? '    ' : ''}    ${printReceived(received)}`;

    return {message, pass};
  },

  toBeInstanceOf(received: any, expected: Function) {
    const matcherName = 'toBeInstanceOf';
    const options: MatcherHintOptions = {
      isNot: this.isNot,
      promise: this.promise,
    };

    if (typeof expected !== 'function') {
      throw new TypeError(
        matcherErrorMessage(
          matcherHint(matcherName, undefined, undefined, options),
          `${EXPECTED_COLOR('expected')} value must be a function`,
          printWithType('Expected', expected, printExpected),
        ),
      );
    }

    const pass = received instanceof expected;

    const message = pass
      ? () =>
          // eslint-disable-next-line prefer-template
          matcherHint(matcherName, undefined, undefined, options) +
          '\n\n' +
          printExpectedConstructorNameNot('Expected constructor', expected) +
          (typeof received.constructor === 'function' &&
          received.constructor !== expected

View on GitHub (pinned to 8e6d128e4a)

Solutions

  1. Pass the constructor/class itself, not an instance: toBeInstanceOf(Error), not toBeInstanceOf(new Error()).
  2. Check the import resolves: confirm the class is exported and the import path/spelling is correct (a failed import gives undefined).
  3. If testing by name, switch to toHaveProperty('constructor.name', 'Error') or a custom check instead of toBeInstanceOf.
  4. For circular-import undefined bindings, move the import or restructure so the class is defined at assertion time.

Example fix

// before
import {Er ror} from './errors'; // typo -> undefined
expect(err).toBeInstanceOf(Er ror);

// after
import {MyError} from './errors';
expect(err).toBeInstanceOf(MyError);
// pass the class, not an instance
expect(err).toBeInstanceOf(Error);
Defensive patterns

Strategy: type-guard

Validate before calling

const ctor = ExpectedClass;
if (typeof ctor !== 'function') {
  throw new Error('toBeInstanceOf expected must be a constructor (function)');
}
expect(received).toBeInstanceOf(ctor);

Type guard

const isConstructor = (v: unknown): v is new (...args: any[]) => any =>
  typeof v === 'function';

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10). Data as JSON: /api/errors/745e839289788581. Report an issue: GitHub.