jest: bug: `resetAllMocks` messes up `runAllTimers` when using `useFakeTimers`
Do you want to request a feature or report a bug? Bug
What is the current behavior?
When using useFakeTimers, running resetAllMocks messes up runAllTimers in the second test (it does not advance the timer) and the test times out (fails).
If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.
https://repl.it/repls/CarelessNormalAssassinbug
What is the expected behavior?
resetAllMocks should indeed reset the calls instances of the setTimeout mock, but runAllTimers should still work as expected and complete the setTimeout
Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system. Jest v22.1.4 Node v9.3.0 Yarn v1.3.2 Mac OS X 10.12.6
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 9
- Comments: 16 (8 by maintainers)
I was not aware that we made
setTimeoutand friends into spies - I’m not sure how that will work once we start using Lolex (#5165).So for now I’d say we should document the current behaviour. Thoughts @cpojer @thymikee?
it’s a surprise to me that
jest. useFakeTimerscould be clear byjest.resetAllMocks, no document for it and I spent one hour to figure out the root cause. If this is the rule, could you update related documents as well?Yes, the current workaround is to
useFakeTimersonbeforeEach, but that’s not the expected behavior.If I understand correctly,
useFakeTimersmockssetTimeoutand the like with Jest mock functions, so they’re supposed to act like all otherjest.fn()andjest.spyOn()mocks.In that sense, running
resetAllMocksshould reset the calls tracking of the mocks, but keep the actual mock intact.So, the expected behavior would be to run
useFakeTimersonly once and reset the mocks after each test. In which caserunAllTimersshould have worked in my setup.This is also based on what we see in the docs:
https://facebook.github.io/jest/docs/en/timer-mocks.html
If the docs would have noted to run it on
beforeEach, I wouldn’t have a problem with that.But since I followed the examples and that led me to broken code, I consider it a bug.