async()


async( [acceptCallCount ] )

Description: Instruct QUnit to wait for an asynchronous operation.

The callback returned from assert.async() will throw an Error if it is invoked more than once (or more often than the accepted call count, if provided).

This replaces functionality previously provided by QUnit.stop() and QUnit.start().

Examples:

Tell QUnit to wait for the done() call inside the timeout.

1
2
3
4
5
6
7
8
QUnit.test( "assert.async() test", function( assert ) {
var done = assert.async();
var input = $( "#test-input" ).focus();
setTimeout(function() {
assert.equal( document.activeElement, input[0], "Input was focused" );
done();
});
});

Call assert.async() for each operation. Each done callback can be called at most once.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
QUnit.test( "two async calls", function( assert ) {
assert.expect( 2 );
var done1 = assert.async();
var done2 = assert.async();
setTimeout(function() {
assert.ok( true, "test resumed from async operation 1" );
done1();
}, 500 );
setTimeout(function() {
assert.ok( true, "test resumed from async operation 2" );
done2();
}, 150);
});

Set up an async test three exit points. Each done() call adds up to the acceptCallCount. After three calls, the test is done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
QUnit.test( "multiple call done()", function( assert ) {
assert.expect( 3 );
var done = assert.async( 3 );
setTimeout(function() {
assert.ok( true, "first call done." );
done();
}, 500 );
setTimeout(function() {
assert.ok( true, "second call done." );
done();
}, 500 );
setTimeout(function() {
assert.ok( true, "third call done." );
done();
}, 500 );
});