question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

karate-config.js fails when using js functions inside it

See original GitHub issue

The karate-config.js fails every time I add an extra function inside it.

function fn() {
 var env = karate.env; // get system property 'karate.env'
 karate.log('karate.env system property was:', env);
 if (!env) {
   env = 'dev';
 }
 var config = {
   env: env,
   myVarName: 'hello karate',
   baseUrl: 'https://www.kloia.com/',
   myString: base64('example string')
 }
 if (env == 'dev') {
   // customize
   // e.g. config.foo = 'bar';
 } else if (env == 'e2e') {
   // customize
 }
 return config;
}

function base64(creds) {
    return 'Basic string' + creds;
}

In my example I just want to use the base64 function to set up a property in the config. Whenever I try to do that my karate-config.js fails with the following error.

08:56:49.815 [main] ERROR com.intuit.karate - src/test/java/com/ebay/example/Hello.feature:4
* print 'Hello World'
>> src/test/java/com/ebay/example/Hello.feature:3
>> karate-config.js failed
>> js failed:
>>>>
01: (function fn() {
02:  var env = karate.env; // get system property 'karate.env'
03:  karate.log('karate.env system property was:', env);
04:  if (!env) {
05:    env = 'dev';
06:  }
07:  var config = {
08:    env: env,
09:    myVarName: 'hello karate',
10:    baseUrl: 'https://www.kloia.com/'
11: //   myString: base64('example string')
12:  }
13:  if (env == 'dev') {
14:    // customize
15:    // e.g. config.foo = 'bar';
16:  } else if (env == 'e2e') {
17:    // customize
18:  }
19:  return config;
20: }
21: 
22: //function base64(creds) {
23: //    return 'Basic string' + creds;
24: //})
<<<<
org.graalvm.polyglot.PolyglotException: SyntaxError: Unnamed:24:4 Expected ) but found eof
//})
    ^

- org.graalvm.polyglot.Context.eval(Context.java:425)
- com.intuit.karate.graal.JsEngine.evalForValue(JsEngine.java:139)
- com.intuit.karate.graal.JsEngine.eval(JsEngine.java:135)
- com.intuit.karate.core.ScenarioEngine.evalJs(ScenarioEngine.java:1190)
- com.intuit.karate.core.ScenarioRuntime.evalConfigJs(ScenarioRuntime.java:284)
- com.intuit.karate.core.ScenarioRuntime.beforeRun(ScenarioRuntime.java:360)
- com.intuit.karate.core.ScenarioRuntime.run(ScenarioRuntime.java:379)```



[karate-playground.zip](https://github.com/karatelabs/karate/files/9949368/karate-playground.zip)

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Krzysztof-Bednarczykcommented, Nov 7, 2022

That makes things simpler 👍🏻 😛 I’ve inherited this project so I’m just trying to deal with what’s in front of me 😉

0reactions
ptrthomascommented, Nov 7, 2022

but I’ll try to be more specific. please please just have one function in karate-config.js at the “top level” this is how all the examples and documentation are written. I have no idea how your style worked and I would consider it a bug 😃 karate was never designed to support that kind of JS that you are showing

Read more comments on GitHub >

github_iconTop Results From Across the Web

When karate-config.js fails it prints error for each feature
1 Answer 1 ... As you know karate-config.js is executed before every scenario. A problem with this is considered "catastrophic". Karate is ...
Read more >
Scenarios failing due to evaluation of 'karate-config.js ... - GitHub
When running a project with multiple (upwards of 1000) scenarios and a few functions set in karate-config.js , including a cached login call ......
Read more >
Karate | Test Automation Made Simple.
Karate is the only open-source tool to combine API test-automation, mocks, performance-testing and even UI automation into a single, unified framework.
Read more >
How to work with Karate Config.js to Switch Env in Karate API ...
This function is expected to return the JSON object, and that all keys and values for that JSON object will be available as...
Read more >
Karate Configuration : Working with Karate Config.js - Scrolltest
Karate expects a file called karate-config.js to exist in the class path and contains a function JavaScript. This function is expected to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found