javascript async unit test framework

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();
      }
    }
  }


})();
 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值