2

I want to clean a database before running my test cases and I'm just having issues with it running. It just times out and I don't understand - hopefully you can help me :)

In the test case I have the following code block:

beforeEach(() => {
      cy.task("cleanUpDB", {
        sql:"UPDATE SQL GOES HERE"
      })

This then goes to my cypress.config file and the following is executed:

on("task", {
        cleanUpDB({ theQuery }) {
          return new Promise(async(resolve, reject) => {
            try{
              await sql.connect(DBConfig)
            const result = await sql.query(theQuery);
            console.log(result)
            return resolve(result);
          }
          catch (err) {
              // ... error checks
             }
          }
          )
        }
      })

This is the error I get in the test runner: enter image description here

Fody
  • 23,754
  • 3
  • 20
  • 37
Ame
  • 47
  • 6

2 Answers2

1

Based on the documentation of the library, the below code should work.

const sqlQuery = 'UPDATE SQL GOES HERE';

beforeEach(() => {
  cy.task('cleanUpDB', {
    sqlQuery,
  });
});

// Config file

on('task', {
  cleanUpDB({ theQuery }) {
    sql.on('error', (err) => {
      // Error handling
    });
    sql
      .connect(DBConfig)
      .then((pool) => {
        return pool.request.query(theQuery);
      })
      .then((result) => {
        console.log(result);
      })
      .catch((error) => {
        console.log(error);
      });
  },
});
Raju
  • 2,299
  • 2
  • 16
  • 19
  • this returned an error: `The task 'cleanUpDB' returned undefined. You must return a value, null, or a promise that resolves to a value or null to indicate that the task was handled.` – Ame Jul 14 '22 at 07:27
1

You must use the same property name sql inside the task

beforeEach(() => {
cy.task("cleanUpDB", {
  sql:"UPDATE SQL GOES HERE"
})
on("task", {
  cleanUpDB({ sql }) {      // since you wrap the parameter
  ...                       // you must use the property name sql

or just pass in the query directly

beforeEach(() => {
cy.task("cleanUpDB", "UPDATE SQL GOES HERE")
on("task", {
  cleanUpDB(theQuery) {     // name can be anything
  ...
Fody
  • 23,754
  • 3
  • 20
  • 37
  • Thank you so much! I followed my original code and just made sure the property name was the same, it really was that simple! Thanks! – Ame Jul 14 '22 at 07:55