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:
CodePudding user response:
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);
});
},
});
CodePudding user response:
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
...