Skip to content
Latchkey

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.

Karate
js failed:
>>>>
token
<<<<
org.graalvm.polyglot.PolyglotException: ReferenceError: "token" is not defined

Common 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

  1. Locate where the variable should be set (karate-config.js or a Background).
  2. Provide it for the CI env, reading from an env var with a default.
  3. Re-run so the expression resolves.
karate-config.js
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.

feature
* 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.

Frequently asked questions

What causes ""js failed ... ReferenceError""?
The expression references a value defined in karate-config.js only for another env, so in CI the name is undefined.
How do I fix "js failed ... ReferenceError"?
Define the value for the CI environment

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card