Sinon clock.tick不会提前setTimeout的时间 [英] Sinon clock.tick doesn't advance time for setTimeout

查看:16
本文介绍了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的存根未被调用。

推荐答案

将您的测试函数asyncawait设置为Promise之后解析的Promise,以便在fooawait排队的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,其中使用JestJesttimer mocks,但概念是相同的,也适用于MochaSinonfake timers

这篇关于Sinon clock.tick不会提前setTimeout的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆