{"record":{"id":"428008af3de18233","repo":"karatelabs/karate","slug":"eval-needs-one-argument","errorCode":null,"errorMessage":"eval() needs one argument","messagePattern":"eval\\(\\) needs one argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":730,"sourceCode":"                arg = args.length > 1 ? args[1] : null;\n            }\n            Object result = rt.executeJsCall(path, arg);\n            if (sharedScope && result instanceof Map) {\n                // Merge result variables into current scope (only meaningful for single-call results)\n                @SuppressWarnings(\"unchecked\")\n                Map<String, Object> resultMap = (Map<String, Object>) result;\n                for (var entry : resultMap.entrySet()) {\n                    engine.put(entry.getKey(), entry.getValue());\n                }\n            }\n            return result;\n        };\n    }\n\n    private JavaInvokable eval() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"eval() needs one argument\");\n            }\n            return engine.eval(args[0].toString());\n        };\n    }\n\n    /**\n     * karate.expect() - Chai-style BDD assertion API.\n     * <p>\n     * Usage:\n     * <pre>\n     * karate.expect(actual).to.equal(expected)\n     * karate.expect(actual).to.be.a('string')\n     * karate.expect(actual).to.have.property('name')\n     * karate.expect(actual).to.not.equal(unexpected)\n     * </pre>\n     */\n    private JavaCallable expect() {\n        return (context, args) -> {","sourceCodeStart":712,"sourceCodeEnd":748,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L712-L748","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass exactly one argument: a string containing the JS expression to evaluate, e.g. karate.eval('1 + 2').","If the expression comes from a variable, verify it is defined and a string before calling: karate.eval(String(expr)).","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."],"exampleFix":"// before\nkarate.eval()\n\n// after\nkarate.eval('response.headers[\"Content-Type\"]')","handlingStrategy":"validation","validationCode":"// JS, before calling\nif (typeof expr !== 'string' || expr.length === 0) {\n  throw new Error('eval expression must be a non-empty string');\n}\nkarate.eval(expr);","typeGuard":"function isEvalArg(a) { return typeof a === 'string' && a.length > 0; }","tryCatchPattern":"var result;\ntry {\n  result = karate.eval(expr);\n} catch (e) {\n  if (String(e.message).indexOf('eval() needs one argument') !== -1) {\n    throw new Error('eval called without an expression — check the expr variable');\n  }\n  throw e;\n}","preventionTips":["Always pass a literal string or a variable you have asserted is a non-empty string.","Avoid building expressions via template substitution that can silently produce undefined.","Prefer karate's typed APIs (karate.get/set) over eval for simple variable access."],"tags":["javascript","argument-count","karate"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}