I want to connect to Mysql DB in the server to delete several records. I have tried using some of the tutorials and answers available. Since all of them are regarding older versions of Cypress, I can't figure out what is the mistake. Here is the code. I'm happy to know if there is any easy method with less code or easy configurations. Thanks
cypress.config.js
const mysql = require("mysql")
function queryTestDb(query, config) {
// creates a new mysql connection using credentials from cypress.json env's
const connection = mysql.createConnection(config.env.db);
// start connection to db
connection.connect();
// exec query + disconnect to db as a Promise
return new Promise((resolve, reject) => {
connection.query(query, (error, results) => {
if (error) reject(error);
else {
connection.end();
// console.log(results)
return resolve(results);
}
});
});
}
module.exports = defineConfig({
e2e: {
db: {
host: "10.100.111.25",
user: "xxxx",
password: "xxxxx",
database: "testDB"
},
async setupNodeEvents(on, config) {
// implement node event listeners here
const bundler = createBundler({
plugins: [createEsbuildPlugin(config)],
});
on("file:preprocessor", bundler);
await addCucumberPreprocessorPlugin(on, config);
on("task", {
queryDb: query => {
return queryTestDb(query, config);
}
});
return config;
},
}
})
Test.spec.js
cy.task("queryDb","'select * from data_table where CREATEDDATE like '2022-12-23'")
.then(count => {
expect(count);
cy.log('DB records count is => '+ count);
});
Package.json
"devDependencies": {
"@badeball/cypress-cucumber-preprocessor": "^15.1.2",
"@bahmutov/cypress-esbuild-preprocessor": "^2.1.5",
"cypress": "^12.4.0",
"cypress-cucumber-preprocessor": "^4.3.1",
"cypress-file-upload": "^5.0.8",
"cypress-mochawesome-reporter": "^3.2.3",
"cypress-multi-reporters": "^1.6.2",
"cypress-xpath": "^2.0.1",
"mocha": "^10.2.0",
"mochawesome": "^7.1.3",
"mochawesome-merge": "^4.2.2",
"mysql": "github:mysqljs/mysql"
},
"dependencies": {
"cypress-email-results": "^1.8.0",
"cypress-mysql": "^1.0.0",
"dayjs": "^1.11.5"
},