How to combine multible script files in a one file

I want to handle all script files in a one file. For example I have a test1.js script file and test2.js script file.They have different options and setup functions. But how can I call test1.js and test2.js in a file?

import test1from "../cypress/tests/PackingProcess";
import test2from "../cypress/tests/Slam";
export const options = {};

export default function main() {
    test1();
    test2();
}

When I run this script I m taking below error;

The handle is invalid.
        at reflect.methodValueCall (native)
        at file:///C:/core/main.js:1:0(19)  hint="script exception"

Hello Yusuf. You likely want to use this syntax:

import { test1 } from "../cypress/tests/PackingProcess.js";
import { test2 } from "../cypress/tests/Slam.js";

The file extension is required, as are the curly braces if importing individual functions.

Github Copilot usually omits the file extension when starting a new file - worth keeping an eye on that if you’re using it to auto-complete code :slight_smile:

@Tom yes when I use your advise ,I can call the functions.I m taking Cannot read property error becuse of setup functions .
Main problem is that two function(test1,test2) uses different setup function in each other. I want each function(test1,test2) to use theirs own setup, and I don’t want to add any more setups inside the main function.I am trying to do exactly the below operation inside the main function;
calling and running different k6 scripts from different files in a single file

Is there a way to do this? It can be if there is a sample code? May be my logic is wrong,if there is a true logic I can take your advice

It sounds like you want to use Scenarios.

With it, you can have automation code in separate files from the main entry-point script (i.e. the one you use with k6 run). You would specify which exported function should be run by the VUs in each scenario using the exec property. This exported function can then be configured to first call your setup function before calling whichever other functions you want.

For example:

import { test1, test1setup } from "../cypress/tests/PackingProcess.js";
import { test2, test2setup } from "../cypress/tests/Slam.js";

export const options = {
  scenarios: {
    test1: {
      executor: 'constant-vus',
      vus: 10,
      duration: '10m',
      exec: 'test1main'
    },
    test2: {
      executor: 'constant-vus',
      vus: 10,
      duration: '10m',
      exec: 'test2main'
    },
  }
}

export function test1main() {
  test1setup();
  test1();
}

export function test2main() {
  test2setup();
  test2();
}

If what you’ve got is actually two separate k6 scripts that you can run directly (with k6 run) and you now want to run these side-by-side as part of the same test, then you might need to make some slight changes.

If each file has its own export function setup() (one of the VU test lifecycle stages) then you’ll need to import these with an alias that allows you to distinguish between them:

import test1, { setup as test1setup } from "./test1.js";
import test2, { setup as test2setup } from "./test2.js";

export const options = {
  scenarios: {
    test1: {
      executor: 'constant-vus',
      vus: 1,
      duration: '1s',
      exec: 'test1main'
    },
    test2: {
      executor: 'constant-vus',
      vus: 1,
      duration: '1s',
      exec: 'test2main'
    },
  }
}

export function test1main() {
  test1setup();
  test1();
}

export function test2main() {
  test2setup();
  test2();
}

The export default function will also need a name. This is what test1.js looks like:

export function setup() {
  console.log('setup1');
}

export default function test1() {
  console.log('test1');
}

This allows you to k6 run test.js as well as k6 run main.js when you want to run multiple scenarios in parallel, each with their own VU settings. Do note that with the above, the setup() function would be called by every VU and not just once which is what the exported setup() function would have been doing - it might not work as you expect it to.

I may have misunderstood what you are trying to achieve but hopefully this helps a bit nonetheless :slight_smile:

This is the solution thanks @Tom