jestjs/jest · error · TypeError
any() expects to be passed a constructor function. Please…
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 asymmetric matcher that matches values `instanceof` the given constructor (or by typeof for the built-in wrappers like String, Number). The Any constructor throws a TypeError when sample is undefined — i.e. when expect.any() is called with no argument — because there is no constructor to match against. The message suggests expect.anything() for the common case of wanting to match any non-null/undefined value.
Solutions
- If you want to match any non-null/undefined value, use `expect.anything()` instead of `expect.any()`.
- If you want to match a specific type, pass the constructor: `expect.any(String)`, `expect.any(MyClass)`.
- Check that the constructor reference you passed is actually defined (e.g. the import resolved).
Example fix
// before
expect(user).toEqual({ id: expect.any(), name: expect.any(String) }); // expect.any() throws
// after
expect(user).toEqual({ id: expect.any(Number), name: expect.any(String) });
// or, to allow any non-null value:
expect(user).toEqual({ id: expect.anything(), name: expect.anything() }); Defensive patterns
Strategy: type-guard
Validate before calling
// Ensure a constructor is present before building an Any matcher.
function anyOf(ctor) {
if (ctor === undefined) {
throw new TypeError('Pass a constructor to expect.any, or use expect.anything()');
}
return expect.any(ctor);
} Type guard
function isConstructor(value: unknown): value is new (...args: any[]) => any {
return typeof value === 'function';
}
const matcher = isConstructor(Ctor) ? expect.any(Ctor) : expect.anything(); Prevention
- Use expect.anything() when you want any non-null value.
- Always pass a concrete constructor to expect.any(Constructor).
- Verify imports resolved (not undefined) before using them as constructors.
When it happens
Trigger: Writing `expect.any()` with zero arguments, or passing a variable that evaluates to undefined where a constructor was intended. The guard fires only for the literal undefined case (not null or other falsy values, which become matchers that match via instanceof).
Common situations: Typo intending expect.anything(); passing an imported symbol that failed to import (undefined); dynamically selecting a constructor from a map lookup that misses; copy-pasting a matcher template and forgetting to fill in the constructor.
Related errors
- Expected is not a string
- Expected is not a String or a RegExp
- You must provide an array to
- You must provide an object to
- expect.customEqualityTesters: Must be set to an array of…
AI-assisted analysis of jestjs/jest@8e6d128e4a (2026-08-10).
Data as JSON: /api/errors/14d0cd5ffdac8b7a.
Report an issue: GitHub.
Appendix: 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 8e6d128e4a)