{"record":{"id":"f10dcb250f3afd19","repo":"karatelabs/karate","slug":"expect-needs-at-least-one-argument","errorCode":null,"errorMessage":"expect() needs at least one argument","messagePattern":"expect\\(\\) needs at least one argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":750,"sourceCode":"            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) -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"expect() needs at least one argument\");\n            }\n            return new Expect(args[0], onMatch);\n        };\n    }\n\n    private JavaInvokable remove() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"remove() needs two arguments: variable name and path\");\n            }\n            String varName = args[0].toString();\n            String path = args[1].toString();\n            Object var = engine.get(varName);\n            if (var instanceof Node && path != null && path.startsWith(\"/\")) {\n                // XPath remove on XML\n                Document doc = var instanceof Document ? (Document) var : ((Node) var).getOwnerDocument();\n                Xml.removeByPath(doc, path);\n            } else if ((var instanceof Map || var instanceof List) && path != null) {","sourceCodeStart":732,"sourceCodeEnd":768,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L732-L768","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the actual value: karate.expect(actual).to.equal(expected).","Confirm the variable holding the actual value is defined and not undefined at the call site.","If asserting on a nested field, extract it explicitly first: var v = response.body.foo; karate.expect(v).to.not.equal(null)."],"exampleFix":"// before\nkarate.expect().to.equal(5)\n\n// after\nkarate.expect(response.status).to.equal(200)","handlingStrategy":"validation","validationCode":"// JS, before calling\nif (actual === undefined) {\n  throw new Error('expect() requires a defined actual value');\n}\nkarate.expect(actual).to.equal(expected);","typeGuard":"function isDefined(v) { return v !== undefined && v !== null; }","tryCatchPattern":"try {\n  karate.expect(actual).to.not.equal(unexpected);\n} catch (e) {\n  if (String(e.message).indexOf('expect() needs at least one argument') !== -1) {\n    throw new Error('expect() called without an actual value — check the variable');\n  }\n  throw e; // rethrow assertion failures as-is\n}","preventionTips":["Extract the value into a local variable and check it is defined before wrapping it in expect().","Never call expect() as a placeholder in generated or scaffolded tests.","Distinguish arity errors from assertion failures in catch blocks by message."],"tags":["javascript","assertion","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"}