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"
}
}
}