connect ETIMEDOUT when connecting cypress with DB

Viewed 64

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);
    },
  });
};
0 Answers
Related