I’m trying to export a “json object” defined in my main k6 file and import it in another js file located in a subfolder. This used to work fine in version 0.39.0 of k6 but in 0.40.0 that stopped working for some reason, something in k6 program code must have broken this.
dir structure (k6example.load.js is main file):
C:\k6example\k6example.load.js
C:\k6example\k6example.js
C:\k6example\utils\util-functions.js
Running below causes the error:
k6 run .\k6example.load.js --env TEST_ENVIRONMENT="Performance1"
Error message:
ERRO[0000] TypeError: Cannot read property 'environmentTestDataFile' of undefined
at getRandomTestUser (file:///C:/k6example/utils/util-functions.js:8:57(8))
at file:///C:/k6example/k6example.js:6:13(41)
at go.k6.io/k6/js.(*InitContext).Require-fm (native)
at file:///C:/k6example/k6example.load.js:1:0(17)
at native hint="script exception"
Files in my test project below
k6example.load.js
import { TestRequest } from "./k6example.js";
let testEnvironment = __ENV.TEST_ENVIRONMENT;
let baseURL = "";
switch (testEnvironment) {
case 'Performance1':
baseURL = "https://my.rest.endpoint";
break;
default:
fail('ERROR No parameter for test environment given (TEST_ENVIRONMENT) or environment not supported (supported: Performance1), quitting');
}
export let environment = {
environmentBaseURL: baseURL,
environmentTestDataFile: "./Data/testdata.json",
};
export default function() {
TestRequest();
}
k6example.js
import { group, check } from 'k6';
import http from 'k6/http';
import { getRandomTestUser } from './utils/util-functions.js';
import { environment } from './k6example.load.js';
const user = getRandomTestUser();
export function TestRequest() {
group("TestRequest", function() {
let res, result, params;
params =
{
headers: {
"accept": "application/json",
},
};
console.log("Base URL: " + environment.environmentBaseURL);
res = http.get(environment.environmentBaseURL + "/internal/MyRestFunction", params);
});
}
util-functions.js
import { environment } from '../k6example.load.js'; //those are only two dots in dir tree, for some reason the message board adds one more dot
export function getRandomTestUser() {
//func for getting random user from test data file environment.environmentTestDataFile
// .
// .
let user = "12345679";
console.log("using this data file path: " + environment.environmentTestDataFile);
return user;
}