Can we import js files in another k6 js file with scenarios in it?

I think what you might need to do in order for this to work is to export the function in your scenarios.js file, rather than import it.

export { orderListDownload } from './orderListDownload.js';

While this example isn’t the exact same as what you are trying to do, I think it follows the same pattern. I implemented the handleSummary() function introduced in k6 v0.30.0 as a separate module in order to reuse the code, rather than copying and pasting that function into dozens of script files. In order for k6 to recognize it and execute the function, I had to export it within the main script file. When I simply imported it, k6 did not execute the function at all.

Example:
main-script.js

export { handleSummary } from './handle-summary.js';

//rest of script follows

handle-summary.js

import { jUnit, textSummary } from 'https://jslib.k6.io/k6-summary/0.0.1/index.js';

export function handleSummary(data) {
    console.log('Preparing the end-of-test summary...');

    return {
        'stdout': textSummary(data, { indent: ' ', enableColors: true}), // Show the text summary to stdout...
        '../path/to/junit.xml': jUnit(data), // but also transform it and save it as a JUnit XML...
        'other/path/to/summary.json': JSON.stringify(data), // and a JSON with all the details...
    }
}
1 Like