Skip to content

Fake timers are broken #2707

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

Closed
ColCh opened this issue Jan 26, 2017 · 11 comments
Closed

Fake timers are broken #2707

ColCh opened this issue Jan 26, 2017 · 11 comments

Comments

@ColCh
Copy link
Contributor

ColCh commented Jan 26, 2017

Please take a look:

with jest.runAllTimers
image

and with hack
image

Used jest version is :

v18.1.0

I've debugged it a bit ... it seems that

this._ticks is empty
because
_createMocks is calling, but _fakeSetTimeout is not calling.

but jest's tests for FakeTimers are passing for latest jest from master... strange

@ColCh ColCh changed the title Timers are broken Fake timers are broken Jan 26, 2017
@thymikee
Copy link
Collaborator

Isn't this related? #2467

@ColCh
Copy link
Contributor Author

ColCh commented Jan 26, 2017

It may be, but:

  • I'm using jest.useFakeTimers(); (with "timers": "fake" result is same)
  • I don't use "resetMocks": true
  • I've checked with or without jest.resetModules();

And with every case result was same

I think it's related to jest fn mocking... But I may be wrong

@thymikee
Copy link
Collaborator

Are you able to provide a minimal repro for this case?

@ColCh
Copy link
Contributor Author

ColCh commented Jan 26, 2017

Yes, it's here
https://github.com/ColCh/jest-timers-bug

please use yarn to install dependencies and yarn test for running test

@thymikee
Copy link
Collaborator

Thank you!

@ColCh
Copy link
Contributor Author

ColCh commented Jan 26, 2017

I've just noticed, that it worked fine with pure ES6 class and without React, react-native, react-test-renderer and other deps - only ES6 class and minimum deps like "jest"

But after making WordAnswer a React.Component and require'ing react and other deps, it began this buggy behaviour

P.S. Please, tell me if something is wrong with my deps, not with jest itself

Sorry, I've mixed up hacked version with jest.runAllTimers. It didn't work with ES6 class too

@thymikee
Copy link
Collaborator

Dude, this code is mixing so many of beforeEach and beforeAll in different contexts that I'm not even sure which should be invoked when.

Running jest.useFakeTimers(); in beforeEach() of the runPlatformDependentTests fixes the problem. But generally you shouldn't call beforeAll in the function that is being called from another describe call. It's hard to read, hard to understand, and as we see it now, leads to "buggy" behaviour.

@ColCh
Copy link
Contributor Author

ColCh commented Jan 27, 2017

OMG, that's was actually my fault...

Reason was this little line (setTimeout.mockReset):

diff --git a/src/screens/__tests__/word-answer-test.js b/src/screens/__tests__/word-answer-test.js
index aff822e..d4782e3 100644
--- a/src/screens/__tests__/word-answer-test.js
+++ b/src/screens/__tests__/word-answer-test.js
@@ -17,7 +17,6 @@ describe('WordAnswer', () => {
       addAnswer = jest.fn();
       submitResult = jest.fn();
       nextTask = jest.fn();
-      setTimeout.mockReset();
     });
 
     fit('should call to next task if some task remaining', () => {

Removing it fix my trouble.

Thank you for spending time on this issue!

@thymikee
Copy link
Collaborator

Glad you solved it!

@arvitaly
Copy link

arvitaly commented Oct 4, 2017

Can you show warning when use fakeTimer inside "it"?

@github-actions
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 May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants