{"record":{"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/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/expect/src/asymmetricMatchers.ts#L72-L108","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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)."],"exampleFix":"// before\nexpect(user).toEqual({ id: expect.any(), name: expect.any(String) }); // expect.any() throws\n\n// after\nexpect(user).toEqual({ id: expect.any(Number), name: expect.any(String) });\n// or, to allow any non-null value:\nexpect(user).toEqual({ id: expect.anything(), name: expect.anything() });","handlingStrategy":"type-guard","validationCode":"// Ensure a constructor is present before building an Any matcher.\nfunction anyOf(ctor) {\n  if (ctor === undefined) {\n    throw new TypeError('Pass a constructor to expect.any, or use expect.anything()');\n  }\n  return expect.any(ctor);\n}","typeGuard":"function isConstructor(value: unknown): value is new (...args: any[]) => any {\n  return typeof value === 'function';\n}\n\nconst matcher = isConstructor(Ctor) ? expect.any(Ctor) : expect.anything();","tryCatchPattern":null,"preventionTips":["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."],"tags":["expect","asymmetric-matcher","typeerror","api-misuse"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}