karatelabs/karate · error · RuntimeException

expect() needs at least one argument

Error message

expect() needs at least one argument

What it means

karate.expect(actual) wraps a value in Karate's Expect fluent assertion API (e.g. .to.not.equal(unexpected)). The library throws this error when expect() is invoked with no arguments, since there is no actual value to assert against. It is a fail-fast arity check in the JavaCallable that constructs the Expect object.

Solutions

  1. Pass the actual value: karate.expect(actual).to.equal(expected).
  2. Confirm the variable holding the actual value is defined and not undefined at the call site.
  3. If asserting on a nested field, extract it explicitly first: var v = response.body.foo; karate.expect(v).to.not.equal(null).

Example fix

// before
karate.expect().to.equal(5)

// after
karate.expect(response.status).to.equal(200)
Defensive patterns

Strategy: validation

Validate before calling

// JS, before calling
if (actual === undefined) {
  throw new Error('expect() requires a defined actual value');
}
karate.expect(actual).to.equal(expected);

Type guard

function isDefined(v) { return v !== undefined && v !== null; }

Try / catch

try {
  karate.expect(actual).to.not.equal(unexpected);
} catch (e) {
  if (String(e.message).indexOf('expect() needs at least one argument') !== -1) {
    throw new Error('expect() called without an actual value — check the variable');
  }
  throw e; // rethrow assertion failures as-is
}

Prevention

When it happens

Trigger: Calling karate.expect() with zero arguments — typically when the value variable is undefined in the JS scope, so the argument list is empty, or the call was scaffolded but never filled in.

Common situations: Asserting on a response field assigned from an undefined variable; auto-generated test skeletons with placeholder expect() calls; refactors that renamed the variable but not the argument.

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

Appendix: source

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

            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) -> {
            if (args.length == 0) {
                throw new RuntimeException("expect() needs at least one argument");
            }
            return new Expect(args[0], onMatch);
        };
    }

    private JavaInvokable remove() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("remove() needs two arguments: variable name and path");
            }
            String varName = args[0].toString();
            String path = args[1].toString();
            Object var = engine.get(varName);
            if (var instanceof Node && path != null && path.startsWith("/")) {
                // XPath remove on XML
                Document doc = var instanceof Document ? (Document) var : ((Node) var).getOwnerDocument();
                Xml.removeByPath(doc, path);
            } else if ((var instanceof Map || var instanceof List) && path != null) {

View on GitHub (pinned to a22eb90246)