【发布时间】:2022-03-08 21:16:34
【问题描述】:
我正在开发 plugin/index.ts 文件,我在其中放置异步函数,例如。清除数据库或将文件上传到应用程序,但它开始增长,我想办法让它保持干净和结构化。
在this 视频中,我看到有一种方法可以将函数存储在单独的文件中,然后使用module.exports = { function } 导出它们,然后在index.ts 中使用require 导入它们。
但我不能让它适用于我的情况。
这是我的plugins/index.ts 文件的简单形式:
const uploadDocument = require('./documents');
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on('task', {
clearDatabase: clearDatabase,
uploadDocument: uploadDocument,
});
async function clearDatabase() { ... }
}
我决定将函数uploadDocument的代码移动到plugins/documents.ts文件中:
这就是文件plugins/documents.ts 的样子:
imports...
async function uploadDocument(fileName: string) { ... }
module.exports = { uploadDocument }
当我以这种方式运行任务测试时:
cy.task("uploadDocument", 'Very_light_file.pdf')
我在 Cypress 中收到此错误:
【问题讨论】:
标签: javascript node.js typescript cypress