QUnit.asyncTest()


QUnit.asyncTest( name, test )

Description: Add an asynchronous test to run. The test must include a call to QUnit.start().

For testing asynchronous code, QUnit.asyncTest will automatically stop the test runner and wait for your code to call QUnit.start() to continue.

The assert argument to the callback contains all of QUnit's assertion methods. Use this argument to call your test assertions.

This method used to have an optional second argument called expected. This argument is deprecated and its usage will start throwing errors in 2.0.0. If you're using it, use expect() instead.

Examples:

Example: The following illustrates an asynchronous test that waits 1 second before resuming

1
2
3
4
5
6
7
8
QUnit.asyncTest( "asynchronous test: one second later!", function( assert ) {
expect( 1 );
setTimeout(function() {
assert.ok( true, "Passed and ready to resume!" );
QUnit.start();
}, 1000);
});

Example: A more practical example that must wait for a video element to be loaded before running any assertions.

1
2
3
4
5
6
7
8
9
QUnit.asyncTest( "asynchronous test: video ready to play", function( assert ) {
expect( 1 );
var $video = $( "video" );
$video.on( "canplaythrough", function() {
assert.ok( true, "video has loaded and is ready to play" );
QUnit.start();
});
});