2017-03-06 20 views
1

我有以下sinon.js/chai.js測試我的JavaScript類。爲什麼我的測試函數嘲笑時間在某些環境下永遠不會完成?

describe('tick', function() { 
 
    beforeEach(function() { 
 
     this.clock = sinon.useFakeTimers(); 
 
    }); 
 
    afterEach(function() { 
 
     this.clock = sinon.restore(); 
 
    }); 
 
    it('should increase the time waited for all translators', function() { 
 
     let queue = new TranslatorQueue(); 
 
     queue.join("translator1"); 
 
     this.clock.tick(1000); 
 
     expect(_.head(queue.queue).timeWaiting).to.equal(1); 
 

 
     queue.join("translator2"); 
 
     this.clock.tick(1000); 
 
     expect(_.head(queue.queue).timeWaiting).to.equal(2); 
 
     expect(_.last(queue.queue).timeWaiting).to.equal(1); 
 
    }); 
 
    });

測試通過我的開發機上很好,但測試永遠不會完成並出現在嘗試運行我的臨時服務器上測試時,進入無限循環。我想這與嘲笑計時器有關,但我該如何解決這個問題呢?

回答

1

那麼,寫出來回答你自己的問題的整個事情似乎在這裏工作。

爲了將來的參考,問題是由於sinon.useFakeTimers()比我們可能喜歡的更爲過分。

鑑於我的代碼只使用setInterval()我能得到測試通過改變假通話時同時運行是

this.clock = sinon.useFakeTimers("setInterval");

我找到了相關的信息在這裏:https://github.com/sinonjs/sinon/issues/484

我不是100%確定爲什麼它在我的機器上工作,但不是服務器...

+0

哈哈我喜歡這種情況。它被稱爲[橡皮鴨調試](https://spin.atomicobject.com/2015/08/18/rubber-duck-debugging/):-) – Thatkookooguy

相關問題