I am writing a test in cypress where I hit an endpoint>it saves a token in DB(MySql)>I wrote a query to extract the token(in the test)>The use the token in the next API request and pass it in the body.
My issue is when I am running this test most of the time they connect to DB is timed out and if I do the same through Db Visualer it's working fine. This is really driving me crazy and the test is never executed fully. I spoke to a developer and they said it's probably coz of cypress keeping the connection open and I should try connection pooling(no idea about it). Could someone help me with this problem is it my code or something else how do I fix this. Below is the code I am running for the test
it('Change Password', () => {
cy.ResetPassAPI(globalThis.data.dradminemail);
const query = 'select _Token from tokens WHERE _Member_Id = 25372 and _Token_Type = "resetPassword" ORDER BY _id DESC LIMIT 1';
cy.task('queryDb', query)
.then(resetoken => {
cy.request({
method: 'PATCH',
url: 'https://xxx/xx/xx/changePasswordWithToken',
body: {
confirmPassword: 'xxx',
password: 'xx',
token: resetoken,
uid: 1234,
},
});
});
});
and below is what i am using to connect
////////connect to DB////////////
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) {
connection.end();
reject(error);
} else {
connection.end();
// console.log(results)
return resolve(results);
}
});
});
}
module.exports = (on, config) => {
// Usage: cy.task('queryDb', query)
on('task', {
queryDb: (query) => {
return queryTestDb(query, config);
},
});
};