{"record":{"id":"9c89b14739cc94cf","repo":"karatelabs/karate","slug":"operand-gettextincludingwhitespace-is-not-a-constructor","errorCode":null,"errorMessage":"${operand.getTextIncludingWhitespace()} is not a constructor","messagePattern":"(.+?) is not a constructor","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Interpreter.java","lineNumber":773,"sourceCode":"    // template is itself a MemberExpression whose evaluation is the function\n    // result, and `new` applies to that result with no arguments.\n    private static Object evalNewExpr(Node node, CoreContext context) {\n        // NEW_EXPR -> [NEW, EXPR -> [<operand>]]; evalFnCall expects the EXPR wrapper.\n        Node exprWrap = node.get(1);\n        Node operand = exprWrap.getFirst();\n        if (operand.type != NodeType.FN_TAGGED_TEMPLATE_EXPR) {\n            return evalFnCall(exprWrap, context, true);\n        }\n        // Per spec, `new tag`x`` applies new to the result of the tagged template\n        // invocation, not to the tag itself. Evaluate the tagged template, then\n        // construct the result with no args. (`new tag`x`()` parses as FN_CALL_EXPR\n        // wrapping FN_TAGGED_TEMPLATE_EXPR and goes through the default path above.)\n        Object callable = evalFnTaggedTemplate(operand, context);\n        if (context.isError()) {\n            return Terms.UNDEFINED;\n        }\n        if (!(callable instanceof JsCallable c) || !c.isConstructable()) {\n            throw JsErrorException.typeError(operand.getTextIncludingWhitespace() + \" is not a constructor\");\n        }\n        return invokeAsConstructor(c, new Object[0], operand, context);\n    }\n\n    /**\n     * Construct {@code callable} as if invoked via {@code new}. Used by\n     * {@code Reflect.construct} which has no syntactic Node to thread through;\n     * the engine creates a synthetic placeholder so event tracing has something\n     * to attach to. Throws TypeError if the target is not constructable.\n     */\n    static Object constructFromHost(JsCallable callable, Object[] args, CoreContext context) {\n        if (!callable.isConstructable()) {\n            throw JsErrorException.typeError(\"target is not a constructor\");\n        }\n        return invokeAsConstructor(callable, args, new Node(NodeType.NEW_EXPR), context);\n    }\n\n    private static Object invokeAsConstructor(JsCallable callable, Object[] args, Node node, CoreContext context) {","sourceCodeStart":755,"sourceCodeEnd":791,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Interpreter.java#L755-L791","documentation":"Thrown when a `new X(...)` expression evaluates to a callable that is not constructable (or not a callable at all). Karate's JS engine tracks the [[Construct]] internal slot per callable; arrow functions, object-literal methods, and plain functions defined without construct semantics reject `new`. The error uses the original operand text so the offending expression appears in the message.","triggerScenarios":"Evaluating `new expr()` in Karate JS where expr evaluates to a non-constructable JsCallable — e.g. `new (() => {})()`, `new Math.max()`, or a JS function object defined without constructability.","commonSituations":"Porting Node scripts into Karate feature files that use `new` on arrow functions or built-ins; typos where the intended class name resolves to a helper function or bound method.","solutions":["Use a regular `function` declaration or a class instead of an arrow function when calling with `new`.","Verify with `typeof expr === 'function'` that the operand is a constructor-like callable before using `new`.","Remove `new` if a plain call was intended (e.g. `Math.max(1,2)` needs no `new`)."],"exampleFix":"// before\nvar Point = (x, y) => ({x, y});\nvar p = new Point(1, 2);\n// after\nfunction Point(x, y) { this.x = x; this.y = y; }\nvar p = new Point(1, 2);","handlingStrategy":"type-guard","validationCode":"// before `new`\nif (typeof Expr !== 'function') throw new Error(Expr + ' is not constructable');","typeGuard":"function isConstructable(v) { return typeof v === 'function' && !/^\\s*\\(?[^)]*\\)?\\s*=>/.test(String(v)); }","tryCatchPattern":"try { var o = new Expr(); } catch (e) { /* TypeError: not a constructor — fall back */ o = Expr(); }","preventionTips":["Never apply `new` to arrow functions or object methods","Prefer `class` declarations for anything instantiated","Check `typeof x === 'function'` before dynamic instantiation"],"tags":["javascript","constructor","interpreter"],"backgroundTag":"not-a-constructor","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"}