簡體   English   中英

摩卡單元測試:超時后如何強制停止執行測試

[英]Mocha unit test: how to forcefully stop execution of test after timeout exceeded

我正在使用 mocha 運行單元測試,我想強制超時,如果 function 超過 30000,所以我設置了 this.timeout(30000);

為了驗證它的工作我在測試用例中放置了無限循環,即使超時超過測試也不會返回。 我希望這個測試在超時后停止執行。

這是測試

    it('should forcefully timeout',function(done) {
    this.timeout(30000);
     while(1) {
        for(var i = 0; i < 10000; i++) {
            if( i % 2 == 0 ) {
                console.log("here");
                for(var i=0; i < 10000; i++) {
                    if( i % 2 == 0 ) {
                        console.log("here1");  
                    }
                }
            }
        }

      }
      done();         
  })


  1. async 運行那些可能需要很長時間的功能。
  2. 添加退出標志,當測試完成時,即使異步 function 仍在運行,mocha 也會終止進程。 例如: mocha \"test/**/*.js\" --exit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM