首页 > 解决方案 > Sinon fakeTimer 导致超时?

问题描述

我对 mocha、sinon、chai 等很陌生,我正在尝试编写一个测试,断言一个方法在 1 秒间隔后被调用 x 次。

目前,当我放置:

clock = sinon.useFakeTimers();

在我的 before(async () =>{}) 块中,它会导致该测试超时。

我什至还没有实现断言,但到目前为止这是我的代码。

  describe('and mutiple calls to Lokalise suceed after', () => {
    describe('being rate limited', () => {
      let clock;
      before(async () => {
        clock = sinon.useFakeTimers();
        let promises = [];

        for(let i = 0; i<10; i++){
          promises.push(when.the_handler_is_invoked_because.a_forwarded_task_closed_event_is_received(lokaliseProject, task))
        };
        await Promise.all(promises);
        clock.tick(1000);
      });

      after(() => {
        clock.restore();
        teardown.unmock_everything();
      });

      it('eventually succeeds in creating the pull request', () => {
        expect(lokaliseProject._requestsToDownloadFiles).to.have.lengthOf(10);
        expect(lokaliseProject._requestsToDownloadFiles[0].include_tags).deep.equal(['challenge-api']);
        // Eventual expect for a method to be called x times
      });
    });
  });

我已经删除了一些代码以使其匿名化,但是有什么突出的东西会导致超时吗?

谢谢!

标签: javascripttestingmocha.jssinon

解决方案


推荐阅读