karatelabs/karate · error · RuntimeException

expect().to.be - no such api

Error message

expect().to.be - no such api: ${key}

What it means

The expect().to.be API supports a fixed set of keys (exist, a/an style checks, closeTo, at, etc.). Any other key passed after .to.be makes initExpectToBe throw a RuntimeException naming the unknown key.

Solutions

  1. Replace with Karate-native equivalents: expect(x == true), match x == '#null', expect(x).to.exist for existence
  2. Use match (== '#(notnull)', '#present') for type/null checks
  3. Verify the key against the supported expect() keyword list in docs

Example fix

// before
expect(flag).to.be.true // no such api: true
// after
expect(flag == true)
Defensive patterns

Strategy: validation

Validate before calling

// before expect(x).to.be.<key>, confirm key is supported: exist, a/an-type checks, closeTo, at
var beKeys = ['exist','closeTo','at'];
if (beKeys.indexOf(key) < 0) throw 'unsupported .to.be key: ' + key;

Type guard

function isBoolean(v){ return typeof v === 'boolean'; } // use expect(isBoolean(flag) && flag) instead of .to.be.true

Try / catch

try { expect(val).to.be.closeTo(10, 0.5); } catch (e) { karate.log(e.message); }

Prevention

When it happens

Trigger: expect(x).to.be.<key>(...) with a key outside the switch cases, e.g. expect(x).to.be.true (booleans handled differently), .to.be.ok, .to.be.null, or any typo.

Common situations: Copying Chai.js idioms (.to.be.true, .to.be.null, .to.be.empty) into Karate; typos like 'exits' instead of 'exist'.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/match/Expect.java:320

                }
                handleResult(context, result);
                return new Expect(subject, false, onResult, throwOnFailure, contextSupplier);
            };
            case "closeTo", "approximately" -> (JavaCallable) (context, args) -> {
                Number actual = (Number) subject;
                Number expected = (Number) args[0];
                Number delta = (Number) args[1];
                Result result;
                if (Math.abs(actual.doubleValue() - expected.doubleValue()) <= delta.doubleValue()) {
                    result = Result.PASS;
                } else {
                    result = Result.fail(actual + " not close to " + expected + " ± " + delta);
                }
                handleResult(context, result);
                return new Expect(subject, false, onResult, throwOnFailure, contextSupplier);
            };
            case "at" -> expectToBeAt;
            default -> throw new RuntimeException("expect().to.be - no such api: " + key);
        };
    }

    private SimpleObject initExpectToDeep() {
        return key -> switch (key) {
            case "include" -> match(Match.Type.CONTAINS_DEEP);
            default -> throw new RuntimeException("expect().to.deep - no such api: " + key);
        };
    }

    @SuppressWarnings("unchecked")
    private SimpleObject initExpectToHaveAll() {
        return key -> switch (key) {
            case "keys" -> (JavaCallable) (context, args) -> {
                Result result;
                if (subject instanceof Map<?, ?> map) {
                    result = evaluate(map.keySet(), Match.Type.CONTAINS, args[0]);
                } else {

View on GitHub (pinned to a22eb90246)