Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created January 3, 2011 23:18
Show Gist options
  • Select an option

  • Save cowboy/764161 to your computer and use it in GitHub Desktop.

Select an option

Save cowboy/764161 to your computer and use it in GitHub Desktop.
QUnit: multiple async tests in one test
// Don't do this! Why? It will wait for two seconds, regardless.
asyncTest( "multiple async w/ setTimeout", function() {
expect( 4 );
var url = "http://jsfiddle.net/echo/jsonp/?callback=?";
$.getJSON( url, { a: 1 }, function( data ) {
ok( data, "data is returned from the server" );
equal( data.a, "1", "the value of data.a should be 1" );
});
$.getJSON( url, { b: 2 }, function( data ) {
ok( data, "data is returned from the server" );
equal( data.b, "2", "the value of data.b should be 2" );
});
setTimeout( start, 2000 );
});
// While the timeout is set for two seconds, the test may continue before that.
test( "multiple async w/ counter", function() {
expect( 4 );
stop( 2000 );
// Only call start() when counter is 0.
var counter = 2;
function done() { --counter || start(); }
var url = "http://jsfiddle.net/echo/jsonp/?callback=?";
$.getJSON( url, { a: 1 }, function( data ) {
ok( data, "data is returned from the server" );
equal( data.a, "1", "the value of data.a should be 1" );
done();
});
$.getJSON( url, { b: 2 }, function( data ) {
ok( data, "data is returned from the server" );
equal( data.b, "2", "the value of data.b should be 2" );
done();
});
});
Copy link

ghost commented Aug 22, 2012

Sweet pattern bro!

@julio421
Copy link

julio421 commented Dec 2, 2014

why not simply use stop(2) and replace done(); by start(); ?

the test will wait to have the 2 starts ready to execute tu run...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment