Sinon clock.tick不会提前setTimeout的时间
本教程将介绍Sinon clock.tick不会提前setTimeout的时间的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。
问题描述
我正在为async
函数编写一个测试,该函数执行一系列任务,并在执行更多任务之前等待60秒。我正在尝试使用sinon.useFakeTimers()
跳过这60秒,以便可以在延迟后测试逻辑。
foo.js
module.exports.foo = async f => {
// ... more code ...
await new Promise((resolve, reject) => {
setTimeout(resolve, 60000);
});
// ... more code ...
f();
// ... more code ...
};
test-foo.js
const sinon = require('sinon');
const expect = require('chai').expect;
const { foo } = require('./foo');
describe('Module Foo ', function() {
it('call function after 1 minute', function() {
var clock = sinon.useFakeTimers();
const bar = sinon.stub();
foo(bar);
expect(bar.called).to.be.false;
clock.tick(100000);
expect(bar.called).to.be.true; // this one fails
});
});
我尝试将sinon.useFakeTimers();
放在其他各种位置,但承诺未解析,并且我传递给foo
的存根未被调用。
推荐答案
将您的测试函数async
和await
设置为Promise
之后解析的Promise
,以便在foo
中await
排队的Promise
回调有机会在执行断言之前运行:
const sinon = require('sinon');
const expect = require('chai').expect;
const { foo } = require('./foo');
describe('Module Foo ', function() {
it('call function after 1 minute', async function() { // <= async test function
var clock = sinon.useFakeTimers();
const bar = sinon.stub();
foo(bar);
expect(bar.called).to.be.false; // Success!
clock.tick(100000);
await Promise.resolve(); // <= give queued Promise callbacks a chance to run
expect(bar.called).to.be.true; // Success!
});
});
有关完整的详细信息,请参阅my answer here,其中使用Jest
和Jest
timer mocks,但概念是相同的,也适用于Mocha
和Sinon
fake timers。
好了关于Sinon clock.tick不会提前setTimeout的时间的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。