我正在學習sinon。我的代碼:承諾延遲摩卡
const bluebird = require('bluebird');
const sinon = require('sinon');
const sinonTest = require('sinon-test')(sinon);
sinon.test = sinonTest;
describe('xxx', function _test() {
this.timeout(2000);
it('should', sinon.test(function() {
return new bluebird.Promise((resolve, reject) => {
try {
console.log('123');
resolve();
} catch (err) {
reject(err);
};
})
.then(() => console.log('456'))
.delay(100)
.then(() => console.log('789'))
.then(function() {
})
}));
});
輸出:
xxx
123
456
爲什麼上面的代碼超時,卡在delay
?由於
UPDATE
const bluebird = require('bluebird');
const sinon = require('sinon');
const sinonTest = require('sinon-test')(sinon);
sinon.test = sinonTest;
describe('xxx', function _test() {
this.timeout(2000);
it('should', sinon.test(function() {
return bluebird
.delay(100)
.then(() => console.log('789'));
}));
});
輸出:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves
UPDATE
感謝@Louis。設置useFakeTimers
工作正常。
但我只是困惑。爲什麼在我的項目中,默認情況下useFakeTimers
設置爲true的現有測試沒有問題?如果useFakeTimers
設置爲true,則承諾延遲不能用於sinonTest()
?
順便說一下,當將sinon
從1.17.6
更新爲2.4.1
時,我遇到了這個問題。 謝謝
如果'{useFakeTimers:false}'set,'delay'不起作用? – BAE
我把你的問題中的代碼放在一個新的文件中,編輯添加第二個參數'{useFakeTimers:false}',安裝所需的軟件包並運行Mocha並運行。我得到了所有'console.log'輸出,並且沒有時間。它在我寫回答之前做到了,現在它可以工作。你一定在做錯事。 – Louis
是的。它正在工作。你解決了我的幾個問題。謝謝。但我只是困惑。爲什麼在我的項目中,沒有設置'useFakeTimers'的現有測試沒有問題? – BAE