{"record":{"id":"e1740490350b246c","repo":"karatelabs/karate","slug":"karate-setup-is-not-available-in-this-context","errorCode":null,"errorMessage":"karate.setup() is not available in this context","messagePattern":"karate\\.setup\\(\\) 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":362,"sourceCode":"        }\n        return new KarateConfig();\n    }\n\n    JavaInvokable abort() {\n        return args -> {\n            ScenarioRuntime rt = getRuntime();\n            if (rt != null) {\n                rt.abort();\n            }\n            return null;\n        };\n    }\n\n    JavaInvokable setup() {\n        return args -> {\n            ScenarioRuntime rt = getRuntime();\n            if (rt == null) {\n                throw new RuntimeException(\"karate.setup() is not available in this context\");\n            }\n            String name = args.length > 0 && args[0] != null ? args[0].toString() : null;\n            return rt.executeSetup(name);\n        };\n    }\n\n    JavaInvokable setupOnce() {\n        return args -> {\n            ScenarioRuntime rt = getRuntime();\n            if (rt == null) {\n                throw new RuntimeException(\"karate.setupOnce() is not available in this context\");\n            }\n            String name = args.length > 0 && args[0] != null ? args[0].toString() : null;\n            return rt.executeSetupOnce(name);\n        };\n    }\n\n    /**","sourceCodeStart":344,"sourceCodeEnd":380,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsBase.java#L344-L380","documentation":"karate.setup() invokes a named setup block (executed once for e.g. one-time data seeding). It requires an active ScenarioRuntime; when called from a context without one (boot scripts, non-scenario JS, suite hooks) the library throws this availability error.","triggerScenarios":"karate.setup('myscenario') or karate.setup() inside karate-config.js, a standalone JS eval, or any hook where getRuntime() returns null.","commonSituations":"Trying to run setup blocks from a suite-level or config script; calling karate.setup() in an afterScenario/afterSuite hook; embedding the Karate engine and invoking JS without a scenario in flight.","solutions":["Call karate.setup() from within a scenario (it is designed for scenario-scoped one-time setup)","Pass the optional setup block name only when using named setups: karate.setup('seed-db')","Move one-time data preparation to a dedicated feature executed before the suite instead of config-time JS."],"exampleFix":"// before (karate-config.js)\nkarate.setup('seed-db'); // throws at boot\n// after — inside a feature\nScenario: init\n* karate.setup('seed-db')","handlingStrategy":"try-catch","validationCode":"// JS: invoke setup only from scenario scope\nScenario: init\n* karate.setup('seed-db')","typeGuard":null,"tryCatchPattern":"try { karate.setup('seed-db'); } catch (e) { if (String(e).indexOf('not available in this context') >= 0) karate.warn('karate.setup() requires a scenario runtime'); throw e; }","preventionTips":["Call karate.setup() only inside a running scenario","Do not call setup from karate-config.js or suite hooks","Use named setup blocks: karate.setup('blockName')","For suite-wide preparation, run a dedicated feature before the suite instead"],"tags":["javascript","lifecycle","setup","karate-js"],"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"}