Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: resetAllMocks messes up runAllTimers when using useFakeTimers #5371

Closed
alexilyaev opened this issue Jan 23, 2018 · 16 comments
Closed

bug: resetAllMocks messes up runAllTimers when using useFakeTimers #5371

alexilyaev opened this issue Jan 23, 2018 · 16 comments

Comments

@alexilyaev
Copy link
Contributor

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

@rafaelramalho19
Copy link
Contributor

In your code you've called the jest.useFakeTimers on a beforeAll.

If you replace that with beforeEach and your afterAll with afterEach, it works (https://repl.it/@Rafazelramalho/CarelessNormalAssassinbug)

Do you really require it to be beforeAll and afterAll?

@alexilyaev
Copy link
Contributor Author

Yes, the current workaround is to useFakeTimers on beforeEach, but that's not the expected behavior.

If I understand correctly, useFakeTimers mocks setTimeout and the like with Jest mock functions, so they're supposed to act like all other jest.fn() and jest.spyOn() mocks.
In that sense, running resetAllMocks should reset the calls tracking of the mocks, but keep the actual mock intact.

So, the expected behavior would be to run useFakeTimers only once and reset the mocks after each test. In which case runAllTimers should 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.

@rafaelramalho19
Copy link
Contributor

@SimenB would you suggest updating the docs or looking into this particular issue?

@SimenB
Copy link
Member

SimenB commented Feb 6, 2018

I was not aware that we made setTimeout and 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?

@thymikee
Copy link
Collaborator

thymikee commented Feb 6, 2018

@SimenB haven't thought about that either. Would be quite a breaking change with Lolex.

@SimenB
Copy link
Member

SimenB commented May 14, 2018

FWIW I've set up the Lolex branch to spy on the methods, so no longer breaking in that regard

@SimenB
Copy link
Member

SimenB commented Feb 5, 2019

My current thinking is that we should remove the mocks from the timing functions when using fake timers. Easy enough for people to do jest.spyOn(global, 'setTimeout') if they want.

@springuper
Copy link

it’s a surprise to me that jest. useFakeTimers could be clear by jest.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?

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@SimenB
Copy link
Member

SimenB commented Feb 28, 2022

Modern timers (available jest 26, default jest 27) are not affected by this. PR welcome documenting this for legacy timers, tho

@andrmaz
Copy link

andrmaz commented Apr 27, 2022

@SimenB I would love to give it a try. Can I handle this ?

@hkaur008
Copy link

I would love to give it a try. Can I handle this ?

@ashutosh887
Copy link

I would like to work on this issue

Nerdman4U added a commit to Nerdman4U/jest that referenced this issue Dec 6, 2023
bug: resetAllMocks messes up runAllTimers when using useFakeTimers jestjs#5371
Copy link

github-actions bot commented Jun 9, 2024

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Jun 9, 2024
Copy link

github-actions bot commented Jul 9, 2024

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 9, 2024
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

9 participants