awslabs/llrt · error · Error

"toThrow" expects string, RegExp, function, Error instance…

Error message

"toThrow" expects string, RegExp, function, Error instance or asymmetric matcher, got "${typeof expected}"

What it means

This Error is thrown by JestChaiExpect's `toThrow` matcher in @llrt/expect when the expected value passed to `.toThrow()` is none of the supported types: string, RegExp, function, Error instance, or asymmetric matcher. The library validates the argument type after exhausting all matcher branches (string equality, regex match, class/constructor, error instance, asymmetric matcher) and panics with the runtime `typeof` of the bad value. It exists to give a clear message instead of silently treating an unsupported value as a passing expectation.

Solutions

  1. Pass an Error instance: expect(fn).toThrow(new Error('msg')) instead of an error-shaped plain object.
  2. Pass the message as a string: expect(fn).toThrow('msg') or a RegExp: expect(fn).toThrow(/msg/).
  3. Pass the error class itself: expect(fn).toThrow(MyError).
  4. Log typeof expected before the call to confirm what is actually being passed (often undefined).

Example fix

// before
expect(fn).toThrow({ message: 'boom' }); // object is not supported
// after
expect(fn).toThrow(new Error('boom')); // or: expect(fn).toThrow('boom');
Defensive patterns

Strategy: validation

Validate before calling

const t = typeof expected;
const ok = t === 'string' || t === 'function' || t === 'undefined' || expected instanceof Error || (expected && typeof expected.asymmetricMatch === 'function');
if (!ok) throw new TypeError('toThrow expects string, RegExp, function, Error or asymmetric matcher, got ' + t);

Type guard

function isToThrowArg(v) {
  return typeof v === 'string' || typeof v === 'function' || v instanceof Error || (v !== null && typeof v === 'object' && typeof v.asymmetricMatch === 'function');
}

Prevention

When it happens

Trigger: Calling expect(fn).toThrow(value) where value is e.g. an object, number, array, boolean, null, or undefined — any value whose typeof is not string/function/object-with-Error-shape and which fails the asymmetric-matcher check.

Common situations: Passing an error message built by interpolation that ended up as a non-string, passing an error's `.message` extraction that returned undefined, or copying Jest examples that pass an object literal (Jest accepts Error-like objects; this build only accepts true Error instances).

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at llrt_core/src/modules/js/@llrt/expect/jest-expect.ts:508

        );
      }

      if (
        typeof expected === "object" &&
        "asymmetricMatch" in expected &&
        typeof (expected as any).asymmetricMatch === "function"
      ) {
        const matcher = expected as any as AsymmetricMatcher<any>;
        return this.assert(
          thrown && matcher.asymmetricMatch(thrown),
          "expected error to match asymmetric matcher",
          "expected error not to match asymmetric matcher",
          matcher,
          thrown
        );
      }

      throw new Error(
        `"toThrow" expects string, RegExp, function, Error instance or asymmetric matcher, got "${typeof expected}"`
      );
    }
  );

  def("toSatisfy", function (matcher: Function, message?: string) {
    return this.be.satisfy(matcher, message);
  });

  utils.addProperty(
    chai.Assertion.prototype,
    "resolves",
    function __VITEST_RESOLVES__(this: any) {
      const error = new Error("resolves");
      utils.flag(this, "promise", "resolves");
      utils.flag(this, "error", error);
      const test: any = utils.flag(this, "vitest-test");
      const obj = utils.flag(this, "object");

View on GitHub (pinned to 742fc00b82)