Passing Trend to function that calls another file

Hey,

I am getting an error when passing a Trend to a function. We have a common functions file where we have API calls, and want to pass in pre-defined trends from different files, so that we can add different combinations of tests and use a common file for them all.

when passing in the trend like so, I am getting a “TypeError: Cannot read property ‘add’ of undefined or null”.

We are doing something of the following:

test1.js

export const tc1_trends = new Trend('TC1_Trend');
export function test_case_1 (url) {
    group('Run TC 1', function() {
      testCase(url, tc1_trends );
  });
}

common.js

export function testCase(url , trend) {
      let res = http.post(`${url});
      check(res , { "status is 200": (r) => r.status === 200, });
      trend.add(res .timings.duration);
};

if there’s a better way, that doesn’t involve polluting the “common.js” file with the trend definitions please let me know.

thank you!

Hi @basilzuberi !

Could you please send sanitized full test files for us, not just parts of it, because for me, the following two files just work without error with the k6 run test1.js command:

test1.js:

import { Trend } from 'k6/metrics';
import { group } from 'k6';
import { testCase } from './common.js';

export const tc1_trends = new Trend('TC1_Trend');
export default function() {
   let url = 'https://test.k6.io';
    group('Run TC 1', function() {
      testCase(url, tc1_trends );
  });
}

common.js:

import http from 'k6/http';
import { check} from 'k6';

export function testCase(url , trend) {
      let res = http.post(url);
      check(res , { "status is 200": (r) => r.status === 200, });
      trend.add(res .timings.duration);
}
1 Like

Thanks! I messed up something and didn’t properly pass in one of my functions parameters which is why I was getting an error. It’s good now.

2 Likes