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");
    }
}
Shane Rowatt
  • 1,951
  • 3
  • 27
  • 44

2 Answers2

9

Please use below code in the karate-config.js

function() {    
    return priceBaseUrl='http://localhost:8080';
}
laffuste
  • 16,287
  • 8
  • 84
  • 91
raghuveer
  • 116
  • 1
  • 3
  • you are correct that removing the name of the function makes it work. – Shane Rowatt Feb 14 '19 at 08:11
  • 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 name* – Frank Lee Apr 24 '19 at 06:25
  • I had this same issue when I started because VSCode was telling me to put a function name rather than anonymous. – djangofan Mar 03 '20 at 16:33
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/

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248