I have a method to trigger a pause using a promise with a setTimeout:
pause = ({ time } = {}) => {
const pauseTime = time || 500;
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, pauseTime)
})
}
I want to test it with jest, expecting the timeout function gets the time argument that pause method receives.
jest.useFakeTimers();
const setTimeoutSpy = spyOn(window, 'setTimeout')
const pausePromise = pause({ time: 2500 })
await jest.runAllTimers();
pausePromise.then(() => {
expect(setTimeoutSpy).toHaveBeenCalledTimes(1);
expect(setTimeoutSpy).toHaveBeenCalledWith(2500);
})
jest.useRealTimers();
return pausePromise
I tried with mocks and spies. Also tried without timers and awaiting the promise. But I always get the same result: Expected spy to have been called one time, but it was called zero times.
Any idea?
Thanks!