首页 > 解决方案 > 在 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
        })
    })
}

我将每个步骤的状态保存在功能对象本身中,并在完成所有测试后将其保存在磁盘上。但是,当我使用afterafterEachmocha 事件时,它们会在所有测试完成之前以某种方式执行。

但是如果我it最后使用而不是after事件,那么它会按顺序运行,我会得到想要的结果。我想避免额外it保存特征对象。

on('task', {
//to print logs to console
mydebug (message) {
    console.log( "DEBUG::" , msg);
    return null;
}
});

标签: promisemocha.jscypress

解决方案


推荐阅读