Karate karate-config.js not a js function
Asked Answered
A

2

8

I'm trying use karate for e2e tests and have started with a minimal setup. I want to create some config items in karate-config.js for use in the tests but karate is reporting that file is not a js function and hence the test fails trying to get the config:

Warning: Nashorn engine is planned to be removed from a future JDK release
12:16:35.264 [Test worker] WARN com.intuit.karate - not a js function or feature file: read('classpath:karate-config.js') - [type: NULL, value: null]
---------------------------------------------------------
feature: classpath:karate/insurer.feature
scenarios:  1 | passed:  0 | failed:  1 | time: 0.0163
---------------------------------------------------------
HTML report: (paste into browser to view) | Karate version: 0.9.1
file:/Users/srowatt/dev/repos/api/price-service/build/surefire-reports/karate.insurer.html
---------------------------------------------------------


-unknown-:4 - javascript evaluation failed: priceBaseUrl, ReferenceError: "priceBaseUrl" is not defined in <eval> at line number 1
org.opentest4j.AssertionFailedError: -unknown-:4 - javascript evaluation failed: priceBaseUrl, ReferenceError: "priceBaseUrl" is not defined in <eval> at line number 1

This is my karate-config.js:

function fn() {

    return {
        priceBaseUrl: "http://localhost:8080"
    };
}

This is my insurer.feature test:

Feature: which creates insurers

Background:
  * url priceBaseUrl
  * configure logPrettyRequest = true
  * configure logPrettyResponse = true

Scenario: basic roundtrip 

# create a new insurer
Given path 'insurers'
And request { name: 'Sammy Insurance', companyCode: '99' }
When method post
Then status 201
And match response == { resourceId: '#number', version: 0, createdBy: 'anonymousUser' }

* def insurerId = response.resourceId

# get insurer by resource id
Given path 'insurers', insurerId
When method get
Then status 200
And match response == { id: '#(id)', name: 'Sammy Insurance', companyCode: '99' }

This is the InsurerTest.java test runner:

package karate;

import com.intuit.karate.junit5.Karate;

class InsurerTest {

    @Karate.Test
    public Karate testInsurer() {
        return new Karate().feature("classpath:karate/insurer.feature");
    }
}
Adulate answered 2/2, 2019 at 2:21 Comment(0)
T
9

Please use below code in the karate-config.js

function() {    
    return priceBaseUrl='http://localhost:8080';
}
Trilemma answered 6/2, 2019 at 12:58 Comment(3)
you are correct that removing the name of the function makes it work.Adulate
Had exactly the same problem, this seems to work. Strange thing is: I didn't write that karate-config.js file. It was generated either by the eclipse plugin or the maven archetype with the function nameReopen
I had this same issue when I started because VSCode was telling me to put a function name rather than anonymous.Cryology
I
5

When I see this:

Warning: Nashorn engine is planned to be removed from a future JDK release

I suspect you are on Java 9 or 11 ? To be honest, we haven't fully tested Karate on those versions of Java yet. Would it be possible for you to confirm that Java 8 (maybe 9 / 10 also) is OK.

That said, we are interested in resolving this as soon as possible, so if you can submit a sample project where we can replicate this, please do so: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

EDIT: Karate 1.0 will use GraalVM instead of Nashorn and will run on even JDK 16: https://software-that-matters.com/2021/01/27/7-new-features-in-karate-test-automation-version-1_0/

Ineducable answered 2/2, 2019 at 5:31 Comment(14)
I can confirm that I'm using JDK11 and that removing the name of function that @raghveer suggested makes it work.Adulate
@ShaneRowatt thanks for the update ! this will help others. just FYI - we will be moving to the GraalVM JS engine in the medium-term: twitter.com/ptrthomas/status/1095346113027092480Ineducable
@PeterThomas answer seems more correct. fixes the original problem, not just the symptom. switching from JDK 1.10 to 1.8 fixed my issueTeno
an update - this is fixed in karate version 0.9.3 onwards: github.com/intuit/karate/issues/724 - and Java versions 8 - 12 should workIneducable
I think I am still seeing this warning on Java 11 with karate V0.9.5Digitoxin
@VikasThange yes that is expected. you will see this in 12 as wellIneducable
I just saw a message from VSCode that said VSCode will require minimum version of JDK 11 very soon.Cryology
@Cryology huh ? VSCode ?Ineducable
just in response to your july 6th, 6:12pm comment. sure, 1.8 will continue to work but debugging 1.8 in vscode might become a problem right?Cryology
@Cryology nopeIneducable
I use JDK 1.8.261 and Karate 0.9.5 version. I receive the Nashorn issue as below evaluation of 'karate-config.js' failed: javascript function call failed: Cannot cast jdk.nashorn.internal.runtime.Undefined to java.util.Map is there any specific JDK version to resolve this issue?Bellybutton
@AshokkumarGanesan 1.8.0_112+ should work so maybe you are not using the JDK you think you areIneducable
@PeterThomas when is the plan to come till Java 15 version ? Shall i downgrade to 1.8 ?Weldon
@Gauravkhurana please start using the 1.0 version, we are focusing only on it: github.com/intuit/karate/wiki/1.0-upgrade-guideIneducable

© 2022 - 2024 — McMap. All rights reserved.