karatelabs/karate · error · JsErrorException (typeError)

Cannot use 'in' operator to search for

Error message

Cannot use 'in' operator to search for '${lhs}' in ${rhs}

What it means

The JS `in` operator only works on objects (it checks [[HasProperty]]). Terms.in throws a TypeError when the right-hand side is a primitive (string, number, null, undefined, etc.), matching spec §13.10.1 step 7 and V8's message.

Solutions

  1. Use the correct membership test for primitives: 'abc'.includes('a') for strings, arr.includes(x) for arrays
  2. Ensure the right-hand side is an object: wrap scalars or use karate's map helpers
  3. Guard before evaluating: if (typeof rhs === 'object' && rhs !== null) usesIn = 'k' in rhs

Example fix

// before
if ('error' in response) { ... }
// after (response may be a plain object or null)
if (response && typeof response === 'object' && 'error' in response) { ... }
Defensive patterns

Strategy: type-guard

Validate before calling

if (rhs === null || typeof rhs !== 'object') throw new Error("'in' requires an object rhs");

Type guard

function canUseIn(v) { return v !== null && (typeof v === 'object' || typeof v === 'function'); }

Try / catch

try { has = 'k' in obj; } catch (e) { if (String(e).includes("'in' operator")) has = false; else throw e; }

Prevention

When it happens

Trigger: Evaluating `'x' in 'abc'`, `0 in 5`, `'k' in null`, or `'k' in undefined` in karate-js scripts.

Common situations: Testing whether a map/key exists but passing a JSON-decoded scalar (strings and arrays from JSON responses are common culprits); confusing `in` with Java/Kotlin collection `contains`.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/Terms.java:1256

                if (current == target) {
                    return true;
                }
                current = current.getPrototype();
            }
        }
        return false;
    }

    /**
     * ECMA relational {@code in} — returns {@code true} iff {@code rhs} (or its
     * prototype chain) has a property named {@code ToPropertyKey(lhs)}.
     * Throws {@link JsErrorException#typeError} when {@code rhs} is not an
     * object (per spec §13.10.1 step 7), since the {@code [[HasProperty]]}
     * internal method is only defined on objects.
     */
    static boolean in(Object lhs, Object rhs) {
        if (!(rhs instanceof ObjectLike obj)) {
            throw JsErrorException.typeError(
                    "Cannot use 'in' operator to search for '"
                            + String.valueOf(lhs) + "' in " + String.valueOf(rhs));
        }
        JsSymbol sym = JsSymbol.keyedBy(lhs);
        if (sym != null) {
            ObjectLike walk = obj;
            while (walk != null) {
                if (walk instanceof JsObject jo && jo.hasSymbol(sym)) {
                    return true;
                }
                walk = walk.getPrototype();
            }
            return false;
        }
        String key = toPropertyKey(lhs);
        ObjectLike current = obj;
        while (current != null) {
            if (current.isOwnProperty(key)) {

View on GitHub (pinned to a22eb90246)