Karate "js failed" ReferenceError in a scenario in CI
Karate evaluated a JavaScript expression (in a step, a call, or karate-config.js) and hit a ReferenceError because a variable or function was not defined in scope. In CI this often means a config value that exists locally is absent.
What this error means
A scenario fails with "js failed: ... evaluation error" and "ReferenceError: \"token\" is not defined", pointing at the expression that referenced the missing name.
js failed:
>>>>
token
<<<<
org.graalvm.polyglot.PolyglotException: ReferenceError: "token" is not definedCommon causes
A variable was never set in this environment
The expression references a value defined in karate-config.js only for another env, so in CI the name is undefined.
A missing env var read in config
karate-config.js reads a system env var that is not set in CI, leaving the derived variable undefined when a step uses it.
How to fix it
Define the value for the CI environment
- Locate where the variable should be set (karate-config.js or a Background).
- Provide it for the CI env, reading from an env var with a default.
- Re-run so the expression resolves.
function fn() {
return { token: java.lang.System.getenv('API_TOKEN') || '' };
}Guard optional variables
Use karate.get with a default so a missing variable does not throw a ReferenceError.
* def token = karate.get('token', '')How to prevent it
- Set every environment-specific variable in karate-config.js with defaults.
- Read secrets from env vars and expose them to the CI step.
- Use karate.get with a default for optional values.