{"record":{"id":"0f9cab7169a0382d","repo":"vitest-dev/vitest","slug":"any-expects-to-be-passed-a-constructor-function","errorCode":null,"errorMessage":"any() expects to be passed a constructor function. Please pass one or use anything() to match any object.","messagePattern":"any\\(\\) expects to be passed a constructor function\\. Please pass one or use anything\\(\\) to match any object\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/expect/src/jest-asymmetric-matchers.ts","lineNumber":231,"sourceCode":"            ),\n          ))\n\n    return this.inverse ? !result : result\n  }\n\n  toString() {\n    return `Array${this.inverse ? 'Not' : ''}Containing`\n  }\n\n  getExpectedType() {\n    return 'array'\n  }\n}\n\nexport class Any extends AsymmetricMatcher<any> {\n  constructor(sample: unknown) {\n    if (typeof sample === 'undefined') {\n      throw new TypeError(\n        'any() expects to be passed a constructor function. '\n        + 'Please pass one or use anything() to match any object.',\n      )\n    }\n    super(sample)\n  }\n\n  fnNameFor(func: Function): string {\n    if (func.name) {\n      return func.name\n    }\n\n    const functionToString = Function.prototype.toString\n\n    const matches = functionToString\n      .call(func)\n      .match(/^(?:async)?\\s*function\\s*(?:\\*\\s*)?([\\w$]+)\\s*\\(/)\n    return matches ? matches[1] : '<anonymous>'","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/expect/src/jest-asymmetric-matchers.ts#L213-L249","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nexpect(mock).toHaveBeenCalledWith(expect.any(undefined))\n// after\nexpect(mock).toHaveBeenCalledWith(expect.anything())\n// or, if you have a real class:\nexpect(mock).toHaveBeenCalledWith(expect.any(User))","handlingStrategy":"type-guard","validationCode":"function asAnyOrAnything(sample) {\n  if (sample === undefined) return expect.anything()\n  if (typeof sample !== 'function') {\n    throw new TypeError('expect.any needs a constructor function')\n  }\n  return expect.any(sample)\n}","typeGuard":"function isConstructor(v): v is new (...args: unknown[]) => unknown {\n  return typeof v === 'function'\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["expect","asymmetric-matcher","type-error"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}