网站搜索

typescript - cypress 在异步调用完成之前调用 after all 钩子


在我的测试中,我有异步函数调用

describe('a', () => {

    after(() => {
        console.log('after call');
    });

    it('a', () => {
        myAsyncFunction().then(() => {
            console.log('async call');
        });
    });

});

查看控制台,首先是after call,然后是async call。

如何让 after 真正地在所有异步调用完成后再被调用?

答案#1

你可以传递 Mocha 的 done() 方法,并在异步响应中调用它

describe('a', () => {

    after(() => {
        console.log('after call');
    });

    it('a', (done) => {
        myAsyncFunction().then(() => {
            console.log('async call');
            done();                   // test finishes only after this call is made
                                      // then after() is called
        });
    });

});