Qunit beforeEach,afterEach - async

Mat*_*ner 3 testing asynchronous qunit

从start()开始,在Qunit 2.0中将删除stop(),通过beforeEach,afterEach方法,异步设置和拆卸的替代方法是什么?例如,如果我希望beforeEach等待承诺完成?

jak*_*lla 7

QUnit基本上希望人们停止使用全局方法(不仅仅是start()stop(),而且test(),expect()等).因此,从版本1.16.0开始,您应始终使用全局namespace(QUnit)或assert传递给test()函数的API参数.这包括新的异步控件:

QUnit.test( "testing async action", function( assert ) {  // <-- note the `assert` argument here
    var done = assert.async();  // tell QUnit we're doing async actions and
                                // hold onto the function it returns for later

    setTimeout(function() {  // do some async stuff
        assert.ok( true, "This happened 100 ms later!" );

        done();  // using the function returned from `assert.async()` we 
                 // tell QUnit we're don with async actions
    }, 100);
});
Run Code Online (Sandbox Code Playgroud)

如果您熟悉旧的start()stop()做事方式,您应该看到这非常相似,但更加分散和可扩展.

因为async()方法调用是在assert测试的参数上,所以它不能在beforeEach()函数中使用.如果你有一个如何做到这一点的例子,请发布它,我们可以尝试找出如何以新的方式git它.

UPDATE

我的错误之前,该assert对象被传递到beforeEachafterEach模块上的回调,所以你应该能够做到这一点,你会为测试做同样的逻辑:

QUnit.module('set of tests', {
    beforeEach: function(assert) {
        var done = assert.async();
        doSomethingAsync(function() {
            done(); // tell QUnit you're good to go.
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

(在QUnit 1.17.1中测试)