promise - 在 moch 测试中的事件之后执行之前不等待异步测试完成
问题描述
我正在为赛普拉斯编写文件预处理器。赛普拉斯在内部使用摩卡咖啡。所以我正在动态地从一个特性文件生成 mocha 测试。这是示例代码。
function runTests( features ){
for(let f_i=0; f_i < features.length; f_i++){
const feature = features[f_i];
let scenarioCount = { count: 0 };
describe("Feature: " + feature.statement, () => {
for(let s_i=0; s_i < rule.scenarios.length; s_i++){
const scenario = rule.scenarios[s_i];
runScenario(scenario, scenarioCount);
}
/*
afterEach(() => {
if(scenarioCount.count === feature.stats.total) saveResult(feature);
});
*/
/*
after(() => {
saveResult(feature);
});
*/
it("__After", function(){
cy.then( () => {
saveResult(feature)
});
})
} );//describe ends
}
}
function saveResult(feature){
cy.writeFile(someFileName, feature);
cy.writeFile(someFileName2, feature);
}
function runScenario(scenario, count){
it("test statement", function(){
return cy.then(() => { //return promise
//run all the steps in a scenario
})
})
}
- 此代码在浏览器中运行。因此我应该用它
cy.write()
来保存磁盘上的任何数据。cy
可以在测试功能it
或某些事件(例如after
)中使用。
我将每个步骤的状态保存在功能对象本身中,并在完成所有测试后将其保存在磁盘上。但是,当我使用after
或afterEach
mocha 事件时,它们会在所有测试完成之前以某种方式执行。
但是如果我it
最后使用而不是after
事件,那么它会按顺序运行,我会得到想要的结果。我想避免额外it
保存特征对象。
on('task', {
//to print logs to console
mydebug (message) {
console.log( "DEBUG::" , msg);
return null;
}
});
解决方案
推荐阅读
- javascript - 使用 node.js/Express 从 HTTP 重定向到 HTTPS
- azure - Umbraco 7.13 Azure Web 服务多节点
- ms-access - MS Access VBA 中的 QUOTE 这个词有什么作用?
- linkedin - 无法使用 LinkedIn API v. 2 在 LinkedIn 上共享
- asp.net-core-mvc - 使用 Azure AD 身份验证 (B2C) 显示 Core MVC 的头像
- hibernate - 级联 - 更改也从非拥有方传播?
- sqlalchemy - 如何使用 pyhive sqlalchemy 连接到 Databricks 集群?
- linq - 读取 XML 枚举没有产生任何结果
- python - 从文件加载时打印作为字典存储在列表中的值
- arrays - 如何搜索字符串数组并快速返回特定值?