I am trying to connect to mysql in cypress test cases. But getting an error on when trying to import file mysql-db-connect.js
in steps definition file.
mysql-db-connect.js
function queryTestDb(query, pool) {
// creates a new mysql connection using credentials from cypress.json env's
const connection = createPool.createConnection(pool)
// 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={queryTestDb}
queryTestDb("select * from test LIMIT 5", pool).then(res => console.log(res));
Above queryTestDb()
code works perfectly when execute from same file.
But throws an error when trying to import and run steps definition file
Step definition format
///<reference types="cypress" />
import { Before,After,Given, When, Then, And } from "cypress-cucumber-preprocessor/steps";
import{closeTabs} from '../../../support/utility/tearDown.js';
import {queryTestDb} from '../../../support/utility/mysql-db-connect.js'; ---> ///getting error on this line
const pool = {host: "xxxx", user: "xxxx", password: "xxxx", database: "main", connectionLimit:10 }
Given("connecting to db", function(){
queryTestDb("select * from test LIMIT 5", pool).then(res => console.log(res));
})