QUnit asyncTest does not continue after error

南笙酒味 提交于 2019-12-23 02:14:07

问题


How can I tell QUnit to consider errors during asyncTest as test failures and continue to next test?

here is an example which QUnit stops running after a ReferenceError: jsfiddle


回答1:


Errors in asynchronous tests die silently if they arise while QUnit isn't officially running.

The simplest solution is to wrap every asyncTest contents in a try/catch block that propagates any errors after restarting QUnit. We don't actually have to pollute the code with a million try/catches--we can decorate your existing methods automagically.

For example:

// surrounds any function with a try/catch block to propagate errors to QUnit when
// called during an asyncTest
function asyncTrier(method) {
    return function () {
        try{
            // if the method runs normally, great!
            method();
        } catch (e) {
            // if not, restart QUnit and pass the error on
            QUnit.start();
            throw new (e);
        }
    };
}

QUnit.asyncTest("sample", 1, function () {
        setTimeout(asyncTrier(function(){
           var foo = window.nonexistentobj.toString() + ""; // throws error

           QUnit.ok("foo defined", !!foo)
           QUnit.start();
       }), 1000);
});

Forked your Fiddle, with a sample wrapping method to automatically apply such a try/catch around every asynchronous block: http://jsfiddle.net/bnMWd/4/

(Edit: updated per comments.)



来源:https://stackoverflow.com/questions/19238530/qunit-asynctest-does-not-continue-after-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!