{"record":{"id":"344bb81e5c5faff4","repo":"karatelabs/karate","slug":"karate-callsingle-is-not-available-in-this-context","errorCode":null,"errorMessage":"karate.callSingle() is not available in this context","messagePattern":"karate\\.callSingle\\(\\) is not available in this context","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsBase.java","lineNumber":405,"sourceCode":"                throw new RuntimeException(\"karate.callonce() is not available in this context\");\n            }\n            if (args.length == 0) {\n                throw new RuntimeException(\"karate.callonce() requires at least one argument (feature path)\");\n            }\n            String path = args[0].toString();\n            Object arg = args.length > 1 ? args[1] : null;\n            return rt.executeJsCallOnce(path, arg);\n        };\n    }\n\n    /**\n     * karate.callSingle() - Execute a feature/JS file once per Suite and cache the result.\n     */\n    JavaInvokable callSingle() {\n        return args -> {\n            ScenarioRuntime rt = getRuntime();\n            if (rt == null) {\n                throw new RuntimeException(\"karate.callSingle() is not available in this context\");\n            }\n            if (args.length == 0) {\n                throw new RuntimeException(\"karate.callSingle() requires at least one argument (path)\");\n            }\n            String path = args[0].toString();\n            Object arg = args.length > 1 ? args[1] : null;\n            return rt.executeCallSingle(path, arg);\n        };\n    }\n\n    /**\n     * karate.configure() - Apply configuration from JavaScript.\n     */\n    JavaInvokable configure() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"configure() needs two arguments: key and value\");\n            }","sourceCodeStart":387,"sourceCodeEnd":423,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsBase.java#L387-L423","documentation":"karate.callSingle() runs a feature/JS file once per Suite and caches the result, which requires a ScenarioRuntime to execute and register the cache. Karate throws this when callSingle is used from a JS context where no runtime is bound (getRuntime() returns null).","triggerScenarios":"Calling `karate.callSingle('classpath:...')` from JS evaluated outside any scenario: init scripts, worker/bootstrap threads, or custom embedded JS engines without a ScenarioRuntime.","commonSituations":"Trying to pre-load shared fixtures before the suite runs; calling callSingle from a Java harness embedding the karate JS object; invoking it from teardown/after hooks where the runtime is gone.","solutions":["Call karate.callSingle() from within a running scenario step so a ScenarioRuntime exists.","For true suite-level pre-initialization, use Suite/Runner-level lifecycle hooks instead of JS karate.callSingle().","When embedding the JS engine in Java, bind the current ScenarioRuntime before evaluating scripts that use callSingle.","If called after scenario completion, move the call earlier in the scenario flow."],"exampleFix":"// before: JS evaluated with no scenario bound\nvar token = karate.callSingle('classpath:auth.feature');\n\n// after: inside a scenario\n// * def token = karate.callSingle('classpath:auth.feature')","handlingStrategy":"try-catch","validationCode":"if (typeof rt !== 'undefined' && rt != null) { var v = karate.callSingle('classpath:utils.js'); }","typeGuard":"function canCallSingle() { try { return typeof karate.callSingle === 'function' && !!getRuntime(); } catch (e) { return false; } }","tryCatchPattern":"try { return karate.callSingle(path); } catch (e) { if (String(e.message).includes('not available in this context')) { /* move call into a scenario or Suite hook */ } throw e; }","preventionTips":["Call callSingle from inside a scenario","Use Suite-level hooks for pre-suite initialization","Do not invoke callSingle in teardown/after contexts where the runtime is cleared"],"tags":["javascript","runtime-context","callsingle","karate"],"backgroundTag":"invalid-state-transition","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"}