jestjs/jest · error · TypeError

any() expects to be passed a constructor function. Please pa

Error message

any() expects to be passed a constructor function. Please pass one or use anything() to match any object.

What it means

expect.any(Constructor) builds an Any matcher; the constructor rejects `undefined` because that is almost always a bug (a forgotten import or a variable that resolved to undefined) rather than a meaningful match. The error points you at expect.anything() which is the correct way to say 'match anything (any non-null/non-undefined object)'.

Source

Thrown at packages/expect/src/asymmetricMatchers.ts:90

      // eslint-disable-next-line @typescript-eslint/no-empty-function
      dontThrow: () => {},
      ...getState<MatcherState>(),
      equals,
      isNot: this.inverse,
      utils,
    };
  }

  abstract asymmetricMatch(other: unknown): boolean;
  abstract toString(): string;
  getExpectedType?(): string;
  toAsymmetricMatcher?(): string;
}

class Any extends AsymmetricMatcher<any> {
  constructor(sample: unknown) {
    if (sample === undefined) {
      throw new TypeError(
        'any() expects to be passed a constructor function. ' +
          'Please pass one or use anything() to match any object.',
      );
    }
    super(sample);
  }

  asymmetricMatch(other: unknown) {
    if (this.sample === String) {
      // eslint-disable-next-line unicorn/no-instanceof-builtins
      return typeof other === 'string' || other instanceof String;
    }

    if (this.sample === Number) {
      // eslint-disable-next-line unicorn/no-instanceof-builtins
      return typeof other === 'number' || other instanceof Number;
    }

View on GitHub (pinned to f49721c78e)

Solutions

  1. Pass a real constructor: expect.any(String), expect.any(Error), expect.any(MyClass).
  2. If you genuinely want to match any object, use expect.anything() instead of expect.any().
  3. If the constructor is an import, verify the import resolved (check the export name and module path).

Example fix

// before — undefinedConstructor is undefined at runtime
expect(mock).toHaveBeenCalledWith(expect.any(undefinedConstructor));

// after
expect(mock).toHaveBeenCalledWith(expect.any(MyClass));
// or, to match any non-null/undefined value:
expect(mock).toHaveBeenCalledWith(expect.anything());
Defensive patterns

Strategy: type-guard

Validate before calling

if (ctor === undefined) {
  throw new TypeError('expect.any needs a constructor; did you mean expect.anything()?');
}
expect.any(ctor);

Type guard

function isConstructor(x: unknown): x is new (...args: unknown[]) => unknown {
  return typeof x === 'function';
}

Try / catch

// expect.any throws synchronously at construction — guard before, not try/catch

Prevention

When it happens

Trigger: Calling expect.any(undefined), expect.any(missingImport), expect.any(SomeClass) where SomeClass is undefined due to a failed import, or expect.any() with no argument.

Common situations: A renamed/removed export that the import didn't catch (undefined at runtime); TS dynamic import timing; passing a value that turned out to be undefined via optional chaining; copy-paste forgetting to fill in the constructor.

Related errors


AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03). Data as JSON: /data/errors/14d0cd5ffdac8b7a.json. Report an issue: GitHub.