摩卡咖啡的承诺延迟
promise delay in mocha
我目前正在学习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
?谢谢
更新
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
设置为真,promise delay 不能在 sinonTest()
中使用?
顺便说一下,我在将 sinon
从 1.17.6
升级到 2.4.1
时遇到了这个问题。
谢谢
默认情况下,Sinon 创建的沙箱已设置其配置,因此沙箱配置选项 useFakeTimers
为 true
。 (在此 documentation page 中搜索 defaultConfig
。)
这意味着当沙盒生效时,时钟似乎停止了,而 Bluebird 的 delay
永远不会解析。你告诉 sinon-test
通过在配置时传递第二个参数来创建没有假计时器的沙箱。这第二个参数实际上是 Sinon 沙箱的配置对象:
const sinonTest = require('sinon-test')(sinon,
{ useFakeTimers: false });
我还没有尝试过,但是从代码中可以看出,如果您需要一些测试来使用假计时器而一些测试不使用假计时器,您可以同时使用多个配置:
const sinonTest = require('sinon-test');
const wrapper = sinonTest(sinon, { useFakeTimers: false });
const wrapperWithTimers = sinonTest(sinon);
您只需要根据测试的需要使用正确的包装器即可。
您添加了问题:
But I am just confused. Why in my project, there are no problems with the existing tests where useFakeTimers
set to true by default? If useFakeTimers
set true, promise delay cannot be used in sinonTest()
?
默认情况下 useFakeTimers
是 true
,但这不会造成问题,除非您有 依赖于 的代码继续工作适当地。我有很多测试套件,在这些套件中我使用了沙箱,并且在我没有注意关闭假计时器的地方它们工作正常。伪计时器通常 不会阻止 运行 的异步函数。例如,如果您在沙箱生效时执行 fs.readFile
,它应该可以正常工作。它只会影响依赖于时钟的函数,例如 setTimeout
、setInterval
和 Date
.
Bluebird 的 delay
方法受到影响,因为它 calls setTimeout
。
我目前正在学习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
?谢谢
更新
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
设置为真,promise delay 不能在 sinonTest()
中使用?
顺便说一下,我在将 sinon
从 1.17.6
升级到 2.4.1
时遇到了这个问题。
谢谢
默认情况下,Sinon 创建的沙箱已设置其配置,因此沙箱配置选项 useFakeTimers
为 true
。 (在此 documentation page 中搜索 defaultConfig
。)
这意味着当沙盒生效时,时钟似乎停止了,而 Bluebird 的 delay
永远不会解析。你告诉 sinon-test
通过在配置时传递第二个参数来创建没有假计时器的沙箱。这第二个参数实际上是 Sinon 沙箱的配置对象:
const sinonTest = require('sinon-test')(sinon,
{ useFakeTimers: false });
我还没有尝试过,但是从代码中可以看出,如果您需要一些测试来使用假计时器而一些测试不使用假计时器,您可以同时使用多个配置:
const sinonTest = require('sinon-test');
const wrapper = sinonTest(sinon, { useFakeTimers: false });
const wrapperWithTimers = sinonTest(sinon);
您只需要根据测试的需要使用正确的包装器即可。
您添加了问题:
But I am just confused. Why in my project, there are no problems with the existing tests where
useFakeTimers
set to true by default? IfuseFakeTimers
set true, promise delay cannot be used insinonTest()
?
默认情况下 useFakeTimers
是 true
,但这不会造成问题,除非您有 依赖于 的代码继续工作适当地。我有很多测试套件,在这些套件中我使用了沙箱,并且在我没有注意关闭假计时器的地方它们工作正常。伪计时器通常 不会阻止 运行 的异步函数。例如,如果您在沙箱生效时执行 fs.readFile
,它应该可以正常工作。它只会影响依赖于时钟的函数,例如 setTimeout
、setInterval
和 Date
.
Bluebird 的 delay
方法受到影响,因为它 calls setTimeout
。