karatelabs/karate · error · TypeError

Set-like object's size is not a number

Error message

Set-like object's size is not a number

What it means

SetRecord validation per the ES set-union/intersection spec: when a Set-like object is passed to a Set.prototype method, its 'size' property must coerce to a number, but NaN was produced. Fix the Set-like object so 'size' is a numeric value.

Solutions

  1. Give the Set-like a numeric size member (e.g. size: 3).
  2. Use a real Set as the argument so size is always numeric.
  3. Validate Number.isInteger(other.size) before calling the method.

Example fix

// before
const like = { size: 'many', has: k => true, keys: () => [][Symbol.iterator]() };
mySet.difference(like);
// after
const like = { size: 0, has: k => true, keys: () => [][Symbol.iterator]() };
mySet.difference(like);
Defensive patterns

Strategy: validation

Validate before calling

if (!Number.isFinite(Number(other?.size))) throw new TypeError('size must be a number');

Type guard

function hasNumericSize(v) { return v !== null && typeof v === 'object' && Number.isFinite(Number(v.size)); }

Try / catch

try { return mySet.difference(other); } catch (e) { if (String(e).includes("size is not a number")) return mySet.difference(new Set()); throw e; }

Prevention

When it happens

Trigger: set.difference({ size: 'not-a-number', has: fn, keys: fn }); or an object whose size getter returns undefined/NaN.

Common situations: Hand-rolled Set-like objects with string or undefined size; typo like `lenght`-style mistakes on the custom object; getters that fail and return undefined.

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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsSetPrototype.java:167

        SetRecord(ObjectLike setObj, long intSize, JsCallable has, JsCallable keys) {
            this.setObj = setObj;
            this.intSize = intSize;
            this.has = has;
            this.keys = keys;
        }
    }

    private static SetRecord getSetRecord(Object other, Context context) {
        if (!(other instanceof ObjectLike obj)) {
            throw JsErrorException.typeError("Set.prototype method called with non-object");
        }
        CoreContext cc = context instanceof CoreContext c ? c : null;
        Object rawSize = obj.getMember("size", obj, cc);
        Number numSize = Terms.toNumberCoerce(rawSize, cc);
        double d = numSize.doubleValue();
        if (Double.isNaN(d)) {
            throw JsErrorException.typeError("Set-like object's size is not a number");
        }
        // ToIntegerOrInfinity, then RangeError on negative — spec step 7.
        long intSize;
        if (Double.isInfinite(d)) {
            intSize = d > 0 ? Long.MAX_VALUE : Long.MIN_VALUE;
        } else {
            intSize = (long) d;
        }
        if (intSize < 0) {
            throw JsErrorException.rangeError("Set-like object's size is negative");
        }
        Object hasFn = obj.getMember("has", obj, cc);
        if (!(hasFn instanceof JsCallable has)) {
            throw JsErrorException.typeError("Set-like object's 'has' is not callable");
        }
        Object keysFn = obj.getMember("keys", obj, cc);
        if (!(keysFn instanceof JsCallable keys)) {
            throw JsErrorException.typeError("Set-like object's 'keys' is not callable");

View on GitHub (pinned to a22eb90246)