karatelabs/karate · error · RuntimeException

eval() needs one argument

Error message

eval() needs one argument

What it means

karate.eval() runs a JavaScript expression string through the embedded JS engine and returns its value. The library throws this error when eval() is called with no arguments, because there is nothing to evaluate — the engine would receive undefined input. It is a fail-fast arity guard inside KarateJs's JavaInvokable wrapper.

Solutions

  1. Pass exactly one argument: a string containing the JS expression to evaluate, e.g. karate.eval('1 + 2').
  2. If the expression comes from a variable, verify it is defined and a string before calling: karate.eval(String(expr)).
  3. If you meant to evaluate a multi-step script, concatenate the statements into one string or use a separate JS context rather than calling eval() repeatedly.

Example fix

// before
karate.eval()

// after
karate.eval('response.headers["Content-Type"]')
Defensive patterns

Strategy: validation

Validate before calling

// JS, before calling
if (typeof expr !== 'string' || expr.length === 0) {
  throw new Error('eval expression must be a non-empty string');
}
karate.eval(expr);

Type guard

function isEvalArg(a) { return typeof a === 'string' && a.length > 0; }

Try / catch

var result;
try {
  result = karate.eval(expr);
} catch (e) {
  if (String(e.message).indexOf('eval() needs one argument') !== -1) {
    throw new Error('eval called without an expression — check the expr variable');
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling karate.eval() with zero arguments, e.g. karate.eval() — often the result of a variable that should have held the expression string being undefined so the argument is dropped, or a typo'd call left without its string.

Common situations: Building the expression dynamically from a variable (karate.eval(expr) where expr is undefined in JS yields a missing arg); copy-paste refactors that removed the expression; templating errors where the expression was substituted as empty.

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/428008af3de18233. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJs.java:730

                arg = args.length > 1 ? args[1] : null;
            }
            Object result = rt.executeJsCall(path, arg);
            if (sharedScope && result instanceof Map) {
                // Merge result variables into current scope (only meaningful for single-call results)
                @SuppressWarnings("unchecked")
                Map<String, Object> resultMap = (Map<String, Object>) result;
                for (var entry : resultMap.entrySet()) {
                    engine.put(entry.getKey(), entry.getValue());
                }
            }
            return result;
        };
    }

    private JavaInvokable eval() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("eval() needs one argument");
            }
            return engine.eval(args[0].toString());
        };
    }

    /**
     * karate.expect() - Chai-style BDD assertion API.
     * <p>
     * Usage:
     * <pre>
     * karate.expect(actual).to.equal(expected)
     * karate.expect(actual).to.be.a('string')
     * karate.expect(actual).to.have.property('name')
     * karate.expect(actual).to.not.equal(unexpected)
     * </pre>
     */
    private JavaCallable expect() {
        return (context, args) -> {

View on GitHub (pinned to a22eb90246)