Bakhshi
Bakhshi

Reputation: 1339

QUnit asyncTest does not continue after error

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

Upvotes: 1

Views: 544

Answers (1)

mjk
mjk

Reputation: 2453

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.)

Upvotes: 1

Related Questions