我正在学习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