{"id":"14d0cd5ffdac8b7a","repo":"jestjs/jest","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/asymmetricMatchers.ts","lineNumber":90,"sourceCode":"      // eslint-disable-next-line @typescript-eslint/no-empty-function\n      dontThrow: () => {},\n      ...getState<MatcherState>(),\n      equals,\n      isNot: this.inverse,\n      utils,\n    };\n  }\n\n  abstract asymmetricMatch(other: unknown): boolean;\n  abstract toString(): string;\n  getExpectedType?(): string;\n  toAsymmetricMatcher?(): string;\n}\n\nclass Any extends AsymmetricMatcher<any> {\n  constructor(sample: unknown) {\n    if (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  asymmetricMatch(other: unknown) {\n    if (this.sample === String) {\n      // eslint-disable-next-line unicorn/no-instanceof-builtins\n      return typeof other === 'string' || other instanceof String;\n    }\n\n    if (this.sample === Number) {\n      // eslint-disable-next-line unicorn/no-instanceof-builtins\n      return typeof other === 'number' || other instanceof Number;\n    }\n","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/expect/src/asymmetricMatchers.ts#L72-L108","documentation":"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)'.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a real constructor: expect.any(String), expect.any(Error), expect.any(MyClass).","If you genuinely want to match any object, use expect.anything() instead of expect.any().","If the constructor is an import, verify the import resolved (check the export name and module path)."],"exampleFix":"// before — undefinedConstructor is undefined at runtime\nexpect(mock).toHaveBeenCalledWith(expect.any(undefinedConstructor));\n\n// after\nexpect(mock).toHaveBeenCalledWith(expect.any(MyClass));\n// or, to match any non-null/undefined value:\nexpect(mock).toHaveBeenCalledWith(expect.anything());","handlingStrategy":"type-guard","validationCode":"if (ctor === undefined) {\n  throw new TypeError('expect.any needs a constructor; did you mean expect.anything()?');\n}\nexpect.any(ctor);","typeGuard":"function isConstructor(x: unknown): x is new (...args: unknown[]) => unknown {\n  return typeof x === 'function';\n}","tryCatchPattern":"// expect.any throws synchronously at construction — guard before, not try/catch","preventionTips":["Verify imports resolved before using them as the constructor.","Use expect.anything() when you want 'any value', not expect.any(undefined).","In TS, type the helper so undefined is rejected at compile time."],"tags":["expect","asymmetric-matcher","type-error","validation"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}