Use fake timers in test to avoid unreliable test behavior #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Not sure why specifically this is happening, but making this render use fake timers when running effects removes the nondeterminism from this test. I think now this test can also remove the promise flushing, since the effects are invoked and the timeout is executed with the
act
-- not absolutely positive on this one, but I ran it a ton of times and it never failed. This test also still works if the flushing of promises is kept in there too, so no harm done with it if it's necessary for some other reason.Hope this helps! I pretty much always use fake timers whenever I have any set(Timeout|Interval) stuff going on and I've had good luck avoiding stuff like this. I'm not 100% sure if jest still isn't cleaning things up completely, but hopefully, it should be able to reset its own state of
useFakeTimers
easier than it can clear all timeouts