karatelabs/karate · error · RuntimeException

expect().to - no such api:

Error message

expect().to - no such api: 

What it means

Unknown-API guard in initExpectTo: the property chained after expect().to is not a supported continuation (be, have, match, include, etc.). Fires on a misspelled or unsupported expect DSL key; the message appends the invalid key.

Solutions

  1. Map unsupported Chai assertions to Karate equivalents (match expressions, karate.match(), plain JS/Java checks)
  2. Use supported chains: expect(x).to.equal(y), .to.contain(z), .to.match(regex), .not.to...
  3. Fix typos — the message prints the exact unrecognized key

Example fix

// before
expect(fn).to.throw(Error) // no such api: throw
// after
* karate.signal(fnResultAvailable) // or use try/catch style in JS block
Defensive patterns

Strategy: validation

Validate before calling

var toKeys = ['that','and','which','equal','eql','include','contain','deep','be','exist','have','match','not'];
if (toKeys.indexOf(key) < 0) throw 'unsupported .to key: ' + key;

Try / catch

try { expect(x).to.equal(y); } catch (e) { karate.log('unsupported key: ' + e.message); }

Prevention

When it happens

Trigger: expect(x).to.<key>(...) with an unsupported key, e.g. .to.throw(fn), .to.be.a('string') misspelled routing, .to.respondTo, or typos like .to.contians.

Common situations: Porting Chai/Mocha tests (.to.throw, .to.be.a) directly to Karate; typos; using sinon/chai plugin vocabulary.

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

Appendix: source

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

                yield new Expect(subject, false, onResult, throwOnFailure, contextSupplier);
            }
            case "have" -> expectToHave;
            case "match" -> (JavaCallable) (context, args) -> {
                Result result;
                if (subject instanceof String s) {
                    if (args[0] instanceof JsRegex regex) {
                        result = regex.test(s) ? Result.PASS : Result.fail("actual: " + s + ", expected: " + regex);
                    } else {
                        result = Result.fail("not a regex: " + args[0]);
                    }
                } else {
                    result = Result.fail("not a string");
                }
                handleResult(context, result);
                return new Expect(subject, false, onResult, throwOnFailure, contextSupplier);
            };
            case "not" -> new Expect(subject, !negated, onResult, throwOnFailure, contextSupplier).expectTo;
            default -> throw new RuntimeException("expect().to - no such api: " + key);
        };
    }

    // ========== SimpleObject Implementation ==========

    @Override
    public Object jsGet(String key) {
        if ("to".equals(key)) {
            return expectTo;
        }
        if ("not".equals(key)) {
            return new Expect(subject, true, onResult, throwOnFailure, contextSupplier);
        }
        if ("that".equals(key) || "and".equals(key) || "which".equals(key)) {
            return this; // language chains
        }
        if ("be".equals(key) || "is".equals(key)) {
            return expectToBe; // shorthand for .to.be

View on GitHub (pinned to a22eb90246)