karatelabs/karate · error · JsErrorException

BigInt.prototype method called on non-BigInt

Error message

BigInt.prototype method called on non-BigInt

What it means

Calling a BigInt.prototype method (toString, valueOf, etc.) with a `this` that is not a BigInt or BigInt wrapper throws this TypeError. Karate's JS engine names it more explicitly than V8's variants but enforces the same spec rule that the receiver must be a BigInt.

Solutions

  1. Always call methods on an actual BigInt: 5n.toString(2), not a detached reference
  2. If rebinding, ensure `this` is BigInt: BigInt.prototype.toString.call(5n, 2)
  3. Coerce before invoking prototype methods: const asBigInt = (v) => typeof v === 'bigint' ? v : BigInt(v)
  4. Avoid borrowing BigInt methods across types; use typeof checks instead

Example fix

// before
const toStr = BigInt.prototype.toString;
toStr.call(5, 2); // TypeError: non-BigInt this
// after
const toStr = (v, r) => BigInt.prototype.toString.call(typeof v === 'bigint' ? v : BigInt(v), r);
Defensive patterns

Strategy: type-guard

Validate before calling

function callBigIntMethod(v, fn, ...args) {
  if (typeof v !== 'bigint') v = BigInt(v);
  return fn.apply(v, args);
}

Type guard

const isBigInt = (v) => typeof v === 'bigint';

Try / catch

let out;
try { out = detachedFn.call(recv, ...args); } catch (e) {
  if (e instanceof TypeError && /non-BigInt/.test(e.message)) out = BigInt(recv).toString(...args);
  else throw e;
}

Prevention

When it happens

Trigger: BigInt.prototype.valueOf.call(42), extracting a method and calling it unbound (const f = x.toString; f()), using .call/.apply with a plain number or string as this, calling prototype methods on Object.create(BigInt.prototype).

Common situations: Destructuring/aliasing BigInt methods and losing the receiver; generic function-composition utilities that rebind `this`; porting Number method patterns (Number.prototype.toString.call is laxer) to BigInt.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/b70f24a3bdf2c680. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsBigIntPrototype.java:82

            }
            return n.toString(radix);
        }
        return n.toString();
    }

    private Object valueOf(Context context, Object[] args) {
        return asBigInt(context);
    }

    private static BigInteger asBigInt(Context context) {
        Object thisObj = context.getThisObject();
        if (thisObj instanceof JsBigInt jb) {
            return jb.value;
        }
        if (thisObj instanceof BigInteger bi) {
            return bi;
        }
        throw JsErrorException.typeError("BigInt.prototype method called on non-BigInt");
    }

}

View on GitHub (pinned to a22eb90246)