karatelabs/karate · error · RuntimeException

appendTo() needs at least two arguments: list and item(s)

Error message

appendTo() needs at least two arguments: list and item(s)

What it means

Karate's appendTo() JS utility mutates a list in place by adding one or more items. It requires at least two arguments: the target list plus at least one item to append. Fewer arguments throw this error.

Solutions

  1. Provide at least one item: appendTo(list, item)
  2. Append multiple items by passing them together: appendTo(list, a, b)
  3. To flatten a list of items, pass the list as an item so it is addAll'd, or loop appendTo calls

Example fix

// before
karate.appendTo(list);
// after
karate.appendTo(list, 'newItem');
Defensive patterns

Strategy: validation

Validate before calling

if (!list || items.length === 0) throw new Error('appendTo needs a list and at least one item');

Type guard

function canAppendTo(args) { return Array.isArray(args) && args.length >= 2 && Array.isArray(args[0]); }

Try / catch

try { karate.appendTo(list, item); } catch (e) { karate.log('appendTo requires list + item: ' + e.message); }

Prevention

When it happens

Trigger: Calling appendTo() with zero arguments or with only the list (appendTo(list)) and no items.

Common situations: Loop code that conditionally adds items but passes none on the first iteration; mixing up appendTo with append (which returns a new list); forgetting to pass items after refactoring.

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/65ece8f0fb3079d4. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:113

                result.add(first);
            }
            for (int i = 1; i < args.length; i++) {
                Object item = args[i];
                if (item instanceof List) {
                    result.addAll((List<?>) item);
                } else {
                    result.add(item);
                }
            }
            return result;
        };
    }

    @SuppressWarnings("unchecked")
    static JavaInvokable appendTo() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("appendTo() needs at least two arguments: list and item(s)");
            }
            List<Object> list = (List<Object>) args[0];
            for (int i = 1; i < args.length; i++) {
                Object item = args[i];
                if (item instanceof List) {
                    list.addAll((List<?>) item);
                } else {
                    list.add(item);
                }
            }
            return list;
        };
    }

    /**
     * Remove duplicates from a list while preserving order.
     * Usage: karate.distinct([1, 2, 2, 3, 1]) => [1, 2, 3]
     */

View on GitHub (pinned to a22eb90246)