karatelabs/karate · error · TypeError

structuredClone requires at least 1 argument

Error message

structuredClone requires at least 1 argument

What it means

This TypeError is thrown by the structuredClone built-in when invoked with no arguments. structuredClone performs a deep clone of its first argument, so a value to clone is mandatory; the spec has no zero-argument form. Note args[1] (the options bag with transfer) is accepted but ignored in this engine since nothing is transferable.

Solutions

  1. Always pass the value: structuredClone(value)
  2. Guard arity before calling: if (args.length > 0) structuredClone(args[0])
  3. Handle the TypeError in try/catch when arity is dynamic

Example fix

// before
structuredClone(...args); // TypeError when args is empty
// after
if (args.length > 0) structuredClone(args[0]);
Defensive patterns

Strategy: validation

Validate before calling

const safeStructuredClone = (args) => args.length > 0 ? structuredClone(args[0]) : undefined;

Type guard

const canClone = (args) => Array.isArray(args) && args.length > 0;

Try / catch

try { return structuredClone(v); } catch (e) { if (e instanceof TypeError && v === undefined) return v; throw e; }

Prevention

When it happens

Trigger: Calling structuredClone() with an empty argument list, e.g. via a dynamic dispatch Function.apply with an empty array, or a helper that conditionally omits the value.

Common situations: Spread/apply patterns where the source array is empty; wrappers forwarding arguments without checking arity; test code probing the API surface.

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/6f3bc253149d40c9. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsStructuredClone.java:55

 * {@code Set}, {@code RegExp}, {@code Error} and boxed primitives. Objects are
 * memoized by identity, so a cyclic graph clones to an equally cyclic one
 * rather than being rejected.
 * <p>
 * Functions and symbols raise a {@code DataCloneError}. Anything else the
 * engine cannot deep-copy — host Java values reaching JS through the bridge —
 * is passed through by reference rather than rejected; the alternative would
 * make {@code structuredClone} unusable on any object graph that touches Java.
 */
final class JsStructuredClone {

    private static final byte DATA_ATTRS = JsObject.WRITABLE | JsObject.CONFIGURABLE;

    private JsStructuredClone() {
    }

    static Object call(Context context, Object[] args) {
        if (args.length == 0) {
            throw JsErrorException.typeError("structuredClone requires at least 1 argument");
        }
        // args[1] is the options bag carrying `transfer` — nothing in this
        // engine is transferable, so it is accepted and ignored
        return clone(args[0], new IdentityHashMap<>(), context instanceof CoreContext cc ? cc : null);
    }

    private static Object clone(Object value, IdentityHashMap<Object, Object> memo, CoreContext ctx) {
        String type = Terms.typeOf(value);
        if ("function".equals(type) || "symbol".equals(type)) {
            throw dataCloneError(type);
        }
        Object seen = memo.get(value);
        if (seen != null) {
            return seen;
        }
        if (value instanceof JsPrimitive jp) {
            return remember(memo, value, boxed(jp));
        }

View on GitHub (pinned to a22eb90246)