Xk6-sql extension: mysql connection cannot by passed from setup()

I had a test case where I had to create 2 scenarios to insert data to tables. Here is the structure of my code.
I received the TypeError: Cannot read property ‘exec’ of undefined or null from the 2 scenarios. Can anyone explain why the db connection was not able to pass from setup()

main.js

import sql from "k6/x/sql";
export { runtest1 } from "./runtest1.js";
export { runtest2 } from "./runtest2.js";

const testConfig = JSON.parse(open("./conf.json"));
export const options = Object.assign(
  {
    insecureSkipTLSVerify: false,
  },
  testConfig
);

export function setup() {
  const db = sql.open("mysql", "root:@tcp(127.0.0.1:3306)/testing");
  console.log(db);
  return db;
}

export default function () {
  console.log("running test cases...");
}

export function teardown(data) {
  data.db.close();
}

runTest1.js

export function runtest1(data) {
  data.db.exec(
    `INSERT INTO cats1 (name, breed, age) VALUES ('test1-2', 'Doodle', 1)`
  );
}

runTest2.js

export function runtest2(data) {
  data.db.exec(
    `INSERT INTO cats1 (name, breed, age) VALUES ('test2-2', 'Doodle', 2)`
  );
}

config.json

{
  "scenarios": {
    "scenario1": {
      "executor": "shared-iterations",
      "vus": 1,
      "iterations": 1,
      "exec": "runtest1"
    },
    "scenario2": {
      "executor": "shared-iterations",
      "vus": 1,
      "iterations": 1,
      "exec": "runtest2"
    }
  }
}

@alex_1 : As the documentation at Test lifecycle says “You can pass only data (i.e. JSON) between setup and the other stages. You cannot pass functions.”
The xk6-sql example instantiates the db connection as a const outside of the setup() function.

1 Like