karatelabs/karate · error · JsErrorException

Cannot convert undefined to a BigInt

Error message

Cannot convert undefined to a BigInt

What it means

BigInt() called with no arguments has nothing to convert; per spec the conversion of undefined to BigInt is illegal, so Karate throws TypeError 'Cannot convert undefined to a BigInt' from the constructor's call path.

Solutions

  1. Pass a value: BigInt(123) or BigInt('123')
  2. Guard the argument: if (v === undefined) return 0n;
  3. Default the parameter: function toBig(v = 0) { return BigInt(v); }
  4. Fix the caller that stopped supplying the argument

Example fix

// before
const n = BigInt();
// after
const n = BigInt(0);
Defensive patterns

Strategy: validation

Validate before calling

if (v === undefined || v === null) throw new Error('BigInt requires a value');

Type guard

function isBigIntConvertible(x) { return typeof x === 'number' ? Number.isFinite(x) && Number.isInteger(x) : typeof x === 'string' ? /^-?\d+$/.test(x) : typeof x === 'bigint'; }

Try / catch

try { return BigInt(v); } catch (e) { if (String(e.message).includes('undefined')) return 0n; throw e; }

Prevention

When it happens

Trigger: BigInt() with an empty argument list; BigInt(someUndefinedVariable); calling a wrapper that forwards no arguments to BigInt; destructuring that omitted the value.

Common situations: Missing function parameters, JSON fields absent from a payload, refactored code where an argument was renamed, template-generated expressions that dropped the value.

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 karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/c47eb8a41490ecf4. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsBigIntConstructor.java:52

class JsBigIntConstructor extends JsFunction {
    private static final byte METHOD_ATTRS = WRITABLE | CONFIGURABLE | PropertySlot.INTRINSIC;

    JsBigIntConstructor() {
        this.name = "BigInt";
        this.length = 1;
        installIntrinsics();
    }

    private void installIntrinsics() {
        defineOwn("asIntN", new JsBuiltinMethod("asIntN", 2, this::asIntN), METHOD_ATTRS);
        defineOwn("asUintN", new JsBuiltinMethod("asUintN", 2, this::asUintN), METHOD_ATTRS);
        defineOwn("prototype", JsBigIntPrototype.INSTANCE, PropertySlot.INTRINSIC);
    }

    @Override
    public Object call(Context context, Object[] args) {
        if (args.length == 0) {
            throw JsErrorException.typeError("Cannot convert undefined to a BigInt");
        }
        return toBigInt(args[0], (CoreContext) context);
    }

    /**
     * Spec ToBigInt: ToPrimitive with hint "number", then convert. ToPrimitive
     * fires only on the rare ObjectLike path — primitive inputs (Number, String,
     * Boolean, BigInt) skip it entirely.
     */
    static BigInteger toBigInt(Object value, CoreContext context) {
        // Rare path: object → call valueOf / toString to get a primitive
        if (value instanceof ObjectLike) {
            value = Terms.toPrimitive(value, "number", context);
            if (context != null && context.isError()) {
                // Caller will surface the propagated error; sentinel return is OK
                // since the host will throw before reading it.
                return BigInteger.ZERO;
            }

View on GitHub (pinned to a22eb90246)