Hi , I am quite new about CodeceptJS. I am trying to connect my database to run some queries before my API calls. I added my configuration codecept.conf.js file ;
DbHelper: {
“require”: “./node_modules/codeceptjs-dbhelper”
},
- I am using these versions of codeceptJS - this is my package.json file ;
“devDependencies”: {
“codeceptjs”: “^3.5.12”,
“codeceptjs-dbhelper”: “^1.2.2”,
“database-js-mssql”: “^0.3.0”,
“database-js-mysql”: “^1.1.3”,
“mysql”: “^2.18.1”, -
}"dependencies": { "form-data": "^4.0.0", "knex": "^3.1.0", "mysql2": "^3.9.0", "node-fetch": "^3.3.2"
Here is my code ;
const { I, DB } = require(“…/helpers/DB.js”);
// BeforeSuite hook for connecting to the database
BeforeSuite(async ({ I }) => {
console.log(“BeforeSuite hook: Connecting to the database”);
const dbConfig = {
host: “host”,
user: “user”,
password: “password”,
database: “devDB”,
port: 6033,
};
// Connects to the database
try {
I.connect(
“testdb”,
“mysql://${dbConfig.user}:${dbConfig.password}@${dbConfig.host}:${dbConfig.port}/${dbConfig.database}”
);
} catch (error) {
console.error(“Error connecting to the database:”, error.message);
}
});
// AfterSuite hook for disconnecting from the database
AfterSuite(async ({ I }) => {
console.log(“Executing AfterSuite hook”);
console.log(“I object:”, I); // Log the I object
// … rest of the code
await I.removeConnection(“testdb”);
});
// Before hook for executing a query before each test scenario
Before(async ({ I }) => {
console.log(“Before hook: Executing the query before each test scenario”);
// This Before hook will execute the query only once before the first scenario
if (!I._queryExecuted) {
try {
// Run the SQL query
const result = await I.run(
“testdb”,
“SELECT productID FROM table WHERE id = xxx”
);
// Log the result
console.log("Query Result:", result);
// If there are multiple rows, log each row
for (const row of result) {
console.log("External ID:", row.productID);
}
// Set a flag to indicate that the query has been executed
I._queryExecuted = true;
} catch (error) {
console.error("Error executing the query:", error.message);
}
}
});
Feature(“DB TEST”);
Scenario(“should connect to the database and perform a query”, async () => {
try {
// Connect to the database
await DB.connect();
// Perform a query
const results = await DB.query(
"SELECT productID FROM table WHERE id = xxx"
);
// Disconnect from the database
await DB.disconnect();
// Perform assertions or other actions using CodeceptJS methods
console.log(results);
} catch (error) {
console.error(“Error connecting to the database:”, error.message);
}
});
(I changed sensitive data in the code ) and getting this error ;
TypeError: Cannot read properties of undefined (reading ‘unshift’)
at context.AfterSuite (C:\Users\Documents\workspace\vx-codeceptjs-bankingProxy\node_modules\codeceptjs\lib\ui.js:146:21)
at Object. (C:\Users\Documents\workspace\vx-codeceptjs-bankingProxy\tests\DBtest.js:25:1)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions…js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)
at require (node:internal/modules/cjs/helpers:110:18)
at Array.forEach ()
Cannot read properties of undefined (reading ‘unshift’)
Does anyone has idea , what should I do? can someone please help me about it ?
Thanks!