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!
Finally I found the issue. It was a problem with the spy implementation:
spyOn(...
instead of jest.spyOn(...
window
to global
Thanks all for your comments!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With