Mobx: Clean timers used for reaction tracking for tests run

Created on 19 May 2020  ยท  12Comments  ยท  Source: mobxjs/mobx

Intended outcome

Jest runs and finishes when expected.

Actual outcome

After adding an <Observer> to a component and using that component in a test with Jest and react-test-renderer, Jest runs successfully but returns a warning Jest did not exit one second after the test run has completed.

How to reproduce the issue

Repo using expo-init and the basic tabs example
clone, yarn, jest

๐ŸŽ mobx-react-lite ๐Ÿ› bug ๐Ÿ™ help wanted

Most helpful comment

It would be straightforward to expose a 'debug-only' call (for unit-testing use only) that shuts down the timer, and call that from the Jest (or other unit test framework) teardown.

All 12 comments

Thanks for the report. The problem is kinda obvious ...

https://github.com/mobxjs/mobx-react-lite/blob/a38cc5e21dc1b7f9beac33ec030acd4a1e44b7b4/src/reactionCleanupTracking.ts#L56-L60

It's a crutch timer that should take care of lingering reactions, but nobody takes care of that lingering timer ๐Ÿ˜†. In the normal app, it's not a problem because it needs to run all the time and it's ended with app exit.

However, fix is much trickier as we can't exactly detect when the timer is not needed anymore. Reference counting of "live" observers sounds like a dangerous approach too.

The "dirty" solution would be to basically expose the script with afterAll that would clean that timer.

I am open to other suggestions ... @RoystonS @xaviergonz @mweststrate @urugator

Hello, @FredyC .

I'm facing the same problem. Do you have any update about that?

@alanhr Not really, if you read my last comment, there is no clear path on how to proceed here. React is not giving proper tools for handling this.

Hmm, it would be interesting to see how recoil or relay handle this, I'm
pretty sure they have a similar clean up strategy.

On Mon, Jun 29, 2020 at 2:00 PM Daniel K. notifications@github.com wrote:

@alanhr https://github.com/alanhr Not really, if you read my last
comment, there is no clear path on how to proceed here. React is not giving
proper tools for handling this.

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/mobxjs/mobx-react-lite/issues/288#issuecomment-651104020,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAN4NBGKVW7EKT7QCPQGYTTRZCF6FANCNFSM4NEPLZTQ
.

jest.useFakeTimers() is solving the problem.

@liamkernighan this was my solution to solve that problem

@liamkernighan It's cool, but it's more like a workaround. Faking timers can have unforeseen consequences. It's easy to forget about it and then be surprised why is test stuck.

It would be straightforward to expose a 'debug-only' call (for unit-testing use only) that shuts down the timer, and call that from the Jest (or other unit test framework) teardown.

Anyone feel free to PR a clearTimers utility.

P.S. I noticed that in the above example the test example the render tree is never cleaned up, so doing that might help as well.

I'll do this within the next few days.

There's already a resetCleanupScheduleForTests() which mobx-react-lite's own tests use to clean the timers up. I'll just expose that as clearTimers().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joey-lucky picture joey-lucky  ยท  3Comments

mdebbar picture mdebbar  ยท  3Comments

Niryo picture Niryo  ยท  3Comments

josvos picture josvos  ยท  3Comments

giacomorebonato picture giacomorebonato  ยท  3Comments