awslabs/llrt · error · TypeError

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

Error message

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

What it means

Any matches values by constructor function, so its constructor requires a defined sample. If expect.any() is called with undefined (typically by calling it with no arguments), it throws this TypeError and suggests using expect.anything() when any object/value should match.

Solutions

  1. Pass a constructor: expect.any(String), expect.any(Number), expect.any(MyClass).
  2. Use expect.anything() if you want to match any non-null value.
  3. Verify the constructor import/variable is defined (console.log or a module import fix).
  4. Use expect.any(Array) via Array or expect.arrayContaining for arrays as appropriate.

Example fix

// before
expect(payload).toEqual({ id: expect.any() });
// after
expect(payload).toEqual({ id: expect.any(Number) });
Defensive patterns

Strategy: validation

Validate before calling

if (ctor === undefined) throw new TypeError('expect.any requires a constructor; use expect.anything() to match any value');

Type guard

const isCtor = (v: unknown): v is Function => typeof v === 'function';

Try / catch

try { expect(v).toEqual(expect.any(ctor)); } catch (e) { /* ctor was undefined — check imports */ }

Prevention

When it happens

Trigger: expect.any() with no argument, or a variable that evaluates to undefined at call time, e.g. expect.any(SomeType) where SomeType failed to import.

Common situations: Translating from expect.anything() and dropping the constructor argument, a broken import leaving the class undefined, or dynamically-selected constructors that end up undefined.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of awslabs/llrt@742fc00b82 (2026-09-12). Data as JSON: /api/errors/f2b9875c01c696b6. Report an issue: GitHub.

Appendix: source

Thrown at llrt_core/src/modules/js/@llrt/expect/jest-asymmetric-matchers.ts:208

          other.some((another) => equals(item, another, []))
        ));

    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) {
    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 742fc00b82)