问题
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