Jest: Fake timers are broken

Created on 26 Jan 2017  路  10Comments  路  Source: facebook/jest

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

Most helpful comment

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

All 10 comments

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

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

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

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

Thank you!

~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

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.

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!

Glad you solved it!

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

Was this page helpful?
0 / 5 - 0 ratings