'Cypress task fails and complains that task event has not been registered in the plugins file
I am using the cypress cy.task()
method/ function to copy a csv
file from one directory to another directory. Below is my cy.task('copycsvFile')
and relevant code written in support/index.js
file. While running it throws following error;
CypressError: cy.task('copycsvFile') failed with the following error:
The 'task' event has not been registered in the plugins file. You must register it before using cy.task()
Any idea why this is not recognised ?
Node version : v10.15.3, Cypress version : 3.1.5
//sample-spec.js file
cy.task('copycsvFile');
Below is my index.js
file
// support/index.js file
const fs = require('fs');
module.exports = (on) => {
on('task', {
copycsvFile: (Obj)=>{
var fs = require('fs');
fs.createReadStream('C:/Users/SomeName/Downloads/Export_Survey_CSV.csv').pipe(fs.createWriteStream('C:/User/Client/Client - Cypress Web UI Tests/cypress/fixtures/Export_Survey_CSV.csv'));
}
});
};
Solution 1:[1]
At last, I have figured out the answer.
I have added the below code in the wrong location and that was the reason it fails and returned the above error.
Now I have corrected the location and added under plugins/index.js
and works perfectly.
I've also made a small change, ie I added return null
since I didn't have anything to return in my case.
// In my spec file;
cy.task('copycsvFile');
// added below code under.. /plugins/index.js
const fs = require('fs');
module.exports = (on) => {
on("task", {
copycsvFile: (Obj) => {
var fs = require("fs");
fs.createReadStream(
"C:/Users/SomeName/Downloads/Export_Survey_CSV.csv"
).pipe(
fs.createWriteStream(
"C:/User/Client/Client - Cypress Web UI Tests/cypress/fixtures/Export_Survey_CSV.csv"
)
);
return null;
},
});
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eugen Sunic |