QUnit Bug Example

by TwitchBronBron

HTML

<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.14.0.css">
 <div id="qunit"></div>
<div id="qunit-fixture"></div>

JavaScript

function doSomethingAsync(callback){
    //do something asynchronously, maybe talk to a server....then when finished, realize something went wrong and throw an exception
    setTimeout(function(){
        throw "QUnit can't test catching this exception";
        callback();
    }, 100);
}
QUnit.config.reorder = false;

//can't catch it this way
QUnit.asyncTest("Async test that throws exception in callback function", function(assert){
    expect(1);
    try{
        doSomethingAsync(function(){
            assert.ok(false, "The exception did not get thrown.");   
            QUnit.start();
        });
    }catch(e){
        assert.ok(true, "We saw the exception get thrown.");     
    }
    
    setTimeout(function() {
        QUnit.start();
        //this allows the test to finish, and then we find out that an exception got thrown that we didn't catch. 
    }, 500 );
    
});


//can't catch it this way either
QUnit.test("Async test that throws exception in callback function", function(assert){
    assert.throws(function(){
        doSomethingAsync(function(){
            QUnit.start();
        } );
    });
    
    setTimeout(function() {
        //this allows the test to finish, and then we find out that an exception got thrown that we didn't catch. 
    }, 500 );
});



//can't catch it this way either
QUnit.asyncTest("Async test that throws exception in callback function", function(assert){
    assert.throws(function(){
        doSomethingAsync(function(){
            QUnit.start();
        } );
    });
    
    setTimeout(function() {
        //this allows the test to finish, and then we find out that an exception got thrown that we didn't catch. 
        QUnit.start();
    }, 500 );
});