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.
- 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.
- 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)