vitest-dev/vitest · error · TypeError

any() expects to be passed a constructor function. Please pa

Error message

any() expects to be passed a constructor function. Please pass one or use anything() to match any object.

What it means

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.

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 d568f8ce37)

Solutions

  1. Pass a constructor: expect.any(Number), expect.any(String), expect.any(MyClass).
  2. If you want to match any non-null/undefined value, use expect.anything() instead.
  3. Double-check the argument isn't being dropped by a faulty wrapper.

Example fix

// before
expect(mock).toHaveBeenCalledWith(expect.any())
// after
expect(mock).toHaveBeenCalledWith(expect.any(Number))
// or, to match any value:
expect(mock).toHaveBeenCalledWith(expect.anything())
Defensive patterns

Strategy: type-guard

Validate before calling

function asConstructor(v: unknown): new (...args: any[]) => any {
  if (v == null || (typeof v !== 'function' && typeof v !== 'object')) {
    throw new TypeError('expect.any needs a constructor; use expect.anything() for any value')
  }
  return v as any
}
expect(mock).toHaveBeenCalledWith(expect.any(asConstructor(Number)))

Type guard

function isConstructor(v: unknown): v is new (...args: any[]) => any {
  return typeof v === 'function' || (typeof v === 'object' && v !== null && typeof (v as any)[Symbol.hasInstance] === 'function')
}

Prevention

When it happens

Trigger: Calling expect.any() with no argument (sample === undefined), or passing an explicitly undefined value. The constructor checks typeof sample === 'undefined'.

Common situations: 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).

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/0f9cab7169a0382d.json. Report an issue: GitHub.