Cypress任务失败,并抱怨任务事件未在插件文件中注册 [英] Cypress task fails and complains that task event has not been registered in the plugins file

查看:26
本文介绍了Cypress任务失败,并抱怨任务事件未在插件文件中注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cypresscy.task()方法/函数将csv文件从一个目录复制到另一个目录。下面是我的cy.task('copycsvFile')support/index.js文件中写的相关代码。运行时抛出以下错误; CypressError:cy.task(‘copy csvFile’)失败,出现以下错误: The 'task' event has not been registered in the plugins file. You must register it before using cy.task()是否知道无法识别的原因?

节点版本:v10.15.3, Cypress版本:3.1.5

//sample-spec.js文件

cy.task('copycsvFile');
下面是我的index.js文件 //support/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'));
        }
    });
};

推荐答案

我终于找到答案了。
我在错误的位置添加了下面的代码,这就是它失败并返回上述错误的原因。
现在我已经更正了位置,并在plugins/index.js下添加了内容,并且工作正常。

我还做了一个小改动,即我增加了return null,因为我的箱子里没有任何东西可以退货。

//在我的规范文件中;

cy.task('copycsvFile');

//在..下添加了以下代码/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;
        }
    });
};

这篇关于Cypress任务失败,并抱怨任务事件未在插件文件中注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆