karatelabs/karate · error · TypeError

argumentsList must be an iterable object

Error message

argumentsList must be an iterable object

What it means

The third argument of Reflect.construct/Reflect.apply (argumentsList) must be array-like: a List, an Object[], or null/undefined (which becomes an empty list). Karate's listToArray throws this TypeError for any other value — a plain string, number, or non-iterable object. Note the check is structural (List/Object[]) rather than a generic JS-iterable walk, so some iterable-but-not-array values are rejected.

Solutions

  1. Wrap the arguments in an actual array: Reflect.apply(f, this, [a, b])
  2. Convert the value to an array first with Array.from(...) or [...iterable]
  3. If arguments may be absent, pass undefined/null explicitly rather than a non-array value

Example fix

// before
Reflect.apply(fn, this, 'someArg');
// after
Reflect.apply(fn, this, ['someArg']);
Defensive patterns

Strategy: validation

Validate before calling

function asArgList(v) {
  if (v == null) return [];
  if (Array.isArray(v)) return v;
  throw new Error('argumentsList must be an array');
}
Reflect.apply(fn, thisArg, asArgList(rawArgs));

Type guard

const isArgList = v => v == null || Array.isArray(v);
if (!isArgList(argList)) throw new Error('pass a real array as argumentsList');

Try / catch

try {
  return Reflect.construct(C, args);
} catch (e) {
  if (String(e.message).includes('argumentsList')) {
    throw new Error('argumentsList must be an array; got: ' + typeof args);
  }
  throw e;
}

Prevention

When it happens

Trigger: Reflect.apply(fn, this, 'abc'); Reflect.construct(C, 5); passing a Map/Set or other non-array iterable as the argument list; passing a JS object like {0:'a', length:1} that would be array-like in real JS but is not a List here.

Common situations: Forgetting to wrap a single argument in an array (Reflect.apply(f, null, arg) instead of [arg]); spread from a non-array iterable; porting code that used array-likes (arguments objects, NodeLists) that are not represented as arrays in Karate.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsReflect.java:121

        Object thisArg = args.length >= 2 ? args[1] : Terms.UNDEFINED;
        Object[] cArgs = listToArray(args.length >= 3 ? args[2] : null);
        if (context instanceof CoreContext cc) {
            cc.thisObject = thisArg;
        }
        return target.call(context, cArgs);
    }

    private static Object[] listToArray(Object value) {
        if (value == null || value == Terms.UNDEFINED) {
            return new Object[0];
        }
        if (value instanceof List<?> list) {
            return list.toArray();
        }
        if (value instanceof Object[] arr) {
            return arr;
        }
        throw JsErrorException.typeError("argumentsList must be an iterable object");
    }

}

View on GitHub (pinned to a22eb90246)