doc : jest fake timers : expect on setTimeout not working
See original GitHub issueš Bug Report
In https://jestjs.io/fr/docs/timer-mocks, we can see that we can assert that setTimeout has been called once : expect(setTimeout).toHaveBeenCalledTimes(1);
However, if you do this in a test, jest will complain :
expect(received).toHaveBeenCalledTimes(expected)
Matcher error: received value must be a mock or spy function
Received has type: function
Received has value: [Function setTimeout]
216 | //TODO: test refresh works
> 217 | expect(setTimeout).toHaveBeenCalledTimes(1);
| ^
218 | });
219 | });
220 |
at Object.<anonymous> (xxx.spec.ts:217:24)
I think the documentation should be fixed to explain how we can do ā¦
Issue Analytics
- State:
- Created 2 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Timer Mocks - Jest
In the following example we enable fake timers by calling jest. useFakeTimers() . This is replacing the original implementation of setTimeout()Ā ...
Read more >Mocking setTimeout with Jest. | Medium - Marek Rozmus
The first one is setting up fake timers and the second one restores the original JS behaviour. Example 1 ā setTimeout calls some...
Read more >Jest: Timer and Promise don't work well. (setTimeout and ...
Timer Mocks work by replacing functions like setTimeout() with mocks when jest.useFakeTimers() is called. These mocks record the argumentsĀ ...
Read more >Unit Testing Beginners Guide - Part 2 - Spying and fake timers
Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. What does actually means is that Jest had expect us toĀ ......
Read more >Using Fake Timers | Testing Library
In some cases, when your code uses timers ( setTimeout , setInterval , clearTimeout , clearInterval ), your tests may become unpredictable, slowĀ ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
How is one supposed to solve this issue?
My tests start to fail as described in the inital report (i.e. I get a āreceived value must be a mock or spy functionā error when invoking
expect(setTimeout).not.toHaveBeenCalled()
in a test).Adding
jest.spyOn(window, 'setTimeout')
inexplicably produces a āReferenceError: setTimeout is not definedā error:Iām using
testEnvironment: 'jsdom'
. The functionwindow.setTimeout
does exist in the test, so I donāt really understand how it can appear as not defined to the test runner.I went by all the reports about it not working and thought that perhaps it was sacrificed for the fact that relying on an external library greatly simplifies things for Jest.
But actually, I was partially wrong and should have tested it more thoroughly.
After you have enabled the fake timers you can spy on the global:
That said; I do still stand by my comment on it most often being more favourable not to do so.
When you use the modern fake timers, āprocessor timeā should not play into the millisecond timing of when a given task can be expected to run though, because time is entirely faked. So with for example
jest.advanceTimersByTime()
you do have a lot of power.I would also think that tasks under fake timers would run in the natural order they are scheduled in. So if you want to ignore the exact timingā¦ and only care about the orderā¦ then perhaps you can use
jest.runAllTimers()
to fast forward in time and exhaust all the queues, and thentoHaveBeenNthCalledWith()
to verify them?