Asynchronous Methods Within K6 Load Tests

Hello I am using k6 with webpack so that I could have the ability to import external npm package dependencies to help with generating the required test data that I need in order to setup my load tests.

However, I am running into an issue here where I have a private npm package which contains a set of asynchronous methods that I use to generate test data by making calls to several services. I was wondering if there is any way to use this external package, or am I going to have to build out these methods again with the use of the k6 async method ( asyncRequest( method, url, [body], [params] ) | Grafana k6 documentation )

import { buildOptions } from './options';
import { testBffClient } from '@test-automation';

const testName = 'as_loadTest_getEncounters';
export const options = buildOptions(testName);

interface SetupData {
  result: string;
}

export async function setup(): Promise<SetupData> {
  const testClient = new testBffClient(
    'https://test.io',
  );
  const response = await testClient.authenticate('username', 'password');
  return {
    result: response.access_token
  }
}

export default function (data: SetupData): void {
  console.log(data.result);
}

This is my example load script that I have and I’m currently getting this error

ERRO[0000] GoError: file:///Users/elijah.taylor-kuni/Github/activity-service/release-readiness/dist/getUserEncounters.test.js: Line 2:30962 Unexpected token await (and 9 more errors)  hint="script exception"

Hi @elijahtaylorkuni :wave:

Apologies for the late reply :bowing_man:

It’s really hard to say what’s going on without knowing a bit more about what’s going on in the module you’re importing. In general, while k6 is compatible with a lot of npm modules out of the box, it’s also not a guarantee at all.

The reason being that as k6 does not use nodejs, nor deno or any other javascript engine under the hood, any code that depends on a dependency from such runtime will most often be missing and lead to errors.

Another reason being that although the k6 runtime and our team managing it are working hard to add support for more and more ECMAScript standards and specification, it is also possible that some functionalities these libraries depend on are missing.

Finally, if your dependency expects to make http requests, the k6 functions should indeed be used instead. There might be a way to automatically swap implementations using some webpack or esbuild functionalities, but I never tried, and wouldn’t be much help myself :bowing_man: