this is a rather simplied version of some asynchronous unit test framework, it is indeed far away from being able to be used as some real-world, production quality of unit test framework; however, it shows the basic/meat and potatos of what is required of a async unit test suite.
/************************************** *@Summary * the framework that helps do the unit test in a asynchronous way * * this will use a centrallized managed timer to do all the schedulings and so on. * * @File: asyncunit.js * * @Usage: * test(function() { pause(); setTimeout(function() { assert(true, "First test completed"); resume(); }, 100); }); test(function() { pause(); setTimeout(function() { assert(true, "Second test completed"); resume(); }, 200); }); * @TODO: * YOU MAY need to include the declaration of the assert calls.. ***************************************/ (function () { var queue = [], paused = false; this.test = function (fn) { queue.push(fn); runTest(); }; this.pause = function (fn) { paused = true; }; this.resume = function (fn) { paused = false; // in some browser, it does not accept the value of 0 when you pass arguments to setTimeout. // instead value of 1 has the similar effect or running the test immediately. as most of the browser have // the granularity of about 10-15 milli-seconds setTimeout(runTest, 1); }; function runTest() { if (!paused && queue.length) { queue.shift()(); // some test function may call 'pause', so it makes sense to do the check // before continuing the test if (!paused) { resume(); } } } })();