prisoner_of_azkaban
prisoner_of_azkaban

Reputation: 758

stub setTimeout function

I have a function wait

async function wait(time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}

And I call this wait like this: await wait(5000); from a different function.

I am writing unit test cases and it always executes wait and each test case waits for 5s.

How do I stub the setTimeout using Sinon?

I tried:

  // Skip setTimeOut
  clock = sinon.useFakeTimers({
    now: Date.now(),
    toFake: ['setTimeout']
  });
  await clock.tickAsync(4000);
  await Promise.resolve();

But it didn't work.

Upvotes: 5

Views: 5358

Answers (2)

Yauhen
Yauhen

Reputation: 469

Consider using sinon.stub(global, 'setTimeout') for Node.js or sinon.stub(window, 'setTimeout') for browsers instead of useFakeTimers.

It will give you the ability to stub it with setTimeoutStub.callsFake(cb => cb()) and assert pause time:

expect(setTimeoutStub.calledWithExactly(sinon.match.func, expectedPause)).to.eq(true)

Upvotes: 0

Lin Du
Lin Du

Reputation: 102287

Related post: setTimeout not triggered while using Sinon's fake timers

Github issue: https://github.com/sinonjs/fake-timers/issues/194#issuecomment-395224370

You can solve this in two ways.

  1. Consider whether your test case requires a delay of 5000ms.

The unit test should test the code logic, it's not integration tests. So, maybe you just need to make an assertion check the wait function is to be called with parameter. It's enough. We don't need to wait for 5000ms delay in the test case.

  1. If you insist want to use sinon.useFakeTimers() and clock.tick(5000).

From the related post, we can do it like this:

index.ts:

async function wait(time: number, clock?) {
  return new Promise((resolve) => {
    setTimeout(resolve, time);
    clock && clock.tick(time);
  });
}

export async function main(time, /* for testing */ clock?) {
  await wait(time, clock);
  console.log('main');
}

index.test.ts:

import { main } from './';
import sinon, { SinonFakeTimers } from 'sinon';

describe('60617715', () => {
  let clock: SinonFakeTimers;
  beforeEach(() => {
    clock = sinon.useFakeTimers();
  });
  afterEach(() => {
    clock.restore();
  });
  it('should pass', async () => {
    await main(5000, clock);
  });
});

unit test results:

  60617715
main
    ✓ should pass


  1 passing (9ms)

Upvotes: 2

Related Questions