2

I am trying to get a token from the Db in a test in cypress but 8 out of 10 times the connection is timed out attaching the error and stack trace belowError

stack trace

When I try to send the query via Db Visualizer or when I access data via a test web app which is connected to the same Db it is working fine and no timeout wanted to know if its something related to my code or server I am using the below code in my index file 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);
    },
  });
};
ruffainn
  • 83
  • 1
  • 14
  • It seems there was some problem in the server connection, now everything is working fine. – ruffainn Jul 29 '22 at 19:55
  • you tried after sometime and the same code works for you??? I am facing the same issue. tried lot but same issue exist . if you make any changes in connect please share – Rama Oct 21 '22 at 08:43
  • 1
    Well, I didn't make any changes. It seemed some code was making the DB slow which was pushed by some devs not related to my test coz after a new release everything was working fine for me – ruffainn Oct 24 '22 at 15:21

0 Answers0