Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Karate karate-config.js not a js function

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");
    }
}
like image 287
Shane Rowatt Avatar asked Feb 02 '19 02:02

Shane Rowatt


2 Answers

Please use below code in the karate-config.js

function() {    
    return priceBaseUrl='http://localhost:8080';
}
like image 59
raghuveer Avatar answered Sep 29 '22 11:09

raghuveer


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/

like image 30
Peter Thomas Avatar answered Sep 29 '22 11:09

Peter Thomas