{"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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/expect/src/jest-asymmetric-matchers.ts#L213-L249","documentation":"Thrown as a TypeError by the Any asymmetric matcher constructor when sample is undefined. expect.any(Ctor) matches values that are instances of a constructor, so passing undefined (the default for a missing argument) is treated as a misuse and the message suggests expect.anything() for matching any object.","triggerScenarios":"Calling expect.any() with no argument (sample === undefined), or passing an explicitly undefined value. The constructor checks typeof sample === 'undefined'.","commonSituations":"Forgetting to pass the constructor: expect.any() instead of expect.any(Number); a variable that is undefined being passed in; confusing expect.any (type match) with expect.anything() (any non-null).","solutions":["Pass a constructor: expect.any(Number), expect.any(String), expect.any(MyClass).","If you want to match any non-null/undefined value, use expect.anything() instead.","Double-check the argument isn't being dropped by a faulty wrapper."],"exampleFix":"// before\nexpect(mock).toHaveBeenCalledWith(expect.any())\n// after\nexpect(mock).toHaveBeenCalledWith(expect.any(Number))\n// or, to match any value:\nexpect(mock).toHaveBeenCalledWith(expect.anything())","handlingStrategy":"type-guard","validationCode":"function asConstructor(v: unknown): new (...args: any[]) => any {\n  if (v == null || (typeof v !== 'function' && typeof v !== 'object')) {\n    throw new TypeError('expect.any needs a constructor; use expect.anything() for any value')\n  }\n  return v as any\n}\nexpect(mock).toHaveBeenCalledWith(expect.any(asConstructor(Number)))","typeGuard":"function isConstructor(v: unknown): v is new (...args: any[]) => any {\n  return typeof v === 'function' || (typeof v === 'object' && v !== null && typeof (v as any)[Symbol.hasInstance] === 'function')\n}","tryCatchPattern":null,"preventionTips":["Never call expect.any() with zero arguments.","Use expect.anything() when you want to match any non-null value.","Enable strict TypeScript checks on matcher arguments."],"tags":["expect","asymmetric-matcher","assertion","typescript"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}