vitest-dev/vitest · 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
The Any matcher (expect.any) throws a TypeError when constructed with undefined. It expects a constructor function/class so it can compare instances; if you genuinely want to match anything (object, primitive, anything), use expect.anything() instead.
Solutions
- Use expect.anything() if you want to match any non-null/non-undefined value.
- Fix the import so the constructor/class is actually defined (check the export and the import path).
- Add a runtime assert that the symbol is defined before passing it to expect.any.
Example fix
// before expect(mock).toHaveBeenCalledWith(expect.any(undefined)) // after expect(mock).toHaveBeenCalledWith(expect.anything()) // or, if you have a real class: expect(mock).toHaveBeenCalledWith(expect.any(User))
Defensive patterns
Strategy: type-guard
Validate before calling
function asAnyOrAnything(sample) {
if (sample === undefined) return expect.anything()
if (typeof sample !== 'function') {
throw new TypeError('expect.any needs a constructor function')
}
return expect.any(sample)
} Type guard
function isConstructor(v): v is new (...args: unknown[]) => unknown {
return typeof v === 'function'
} Prevention
- Use expect.anything() when the specific constructor does not matter.
- Verify imports resolve to a defined class before passing to expect.any.
- Watch for circular imports that yield undefined at usage time.
When it happens
Trigger: expect.any(sample) where sample is undefined — typically an undefined class due to a missing/bad import, a circular import that returned undefined, or a conditional that produced undefined.
Common situations: Importing a class that does not exist or is not exported from the target module; type-only import that got stripped; referencing a global that is undefined in the current environment.
Related errors
- Expected is not a Number
- Expected is not a string
- Expected is not a String or a RegExp
- Precision is not a Number
- You must provide an array to
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/0f9cab7169a0382d.
Report an issue: GitHub.
Appendix: source
Thrown at packages/expect/src/jest-asymmetric-matchers.ts:231
),
))
return this.inverse ? !result : result
}
toString() {
return `Array${this.inverse ? 'Not' : ''}Containing`
}
getExpectedType() {
return 'array'
}
}
export class Any extends AsymmetricMatcher<any> {
constructor(sample: unknown) {
if (typeof 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)
}
fnNameFor(func: Function): string {
if (func.name) {
return func.name
}
const functionToString = Function.prototype.toString
const matches = functionToString
.call(func)
.match(/^(?:async)?\s*function\s*(?:\*\s*)?([\w$]+)\s*\(/)
return matches ? matches[1] : '<anonymous>'View on GitHub (pinned to 1fa9837ec2)