【发布时间】:2017-08-23 20:19:45
【问题描述】:
我目前正在学习诗乃。我的代码:
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?谢谢
更新
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
更新
感谢@Louis。设置useFakeTimers 工作正常。
但我只是感到困惑。为什么在我的项目中,useFakeTimers 默认设置为 true 的现有测试没有问题?如果useFakeTimers设置为true,sinonTest()中不能使用promise延迟?
顺便说一句,我在将sinon 从1.17.6 升级到2.4.1 时遇到了这个问题。
谢谢
【问题讨论】:
标签: promise mocha.js bluebird sinon