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

Avoid setImmediate calls on Jest environment #4083

Merged
merged 2 commits into from Feb 22, 2023
Merged

Avoid setImmediate calls on Jest environment #4083

merged 2 commits into from Feb 22, 2023

Conversation

kmagiera
Copy link
Member

Summary

This PR adds Jest specific code to places where we'd used setImmediate. We want to avoid calling setImmediate as it makes writing tests a bit tricky.

The reason is that Jest mocks all the timer-related stuff putting all enqueued function in a single bucket. Then, in order to run these enqueued functions, the test needs to call advanceTimersByTime. It isn't allowed to schedule a callback for the same "tick" which results in the chain of setImmediate calls requiring a several separate runs to advanceTimersByTime. This can be seen in the previous workaround where we had three subsequent calls to that method just in order to start a single animation.

With this change we no longer expect from test to run any additional setup / timer-advancing steps when testing reanimated components and animations.

Test plan

Run jest tests.

src/reanimated2/mappers.ts Outdated Show resolved Hide resolved
Co-authored-by: Tomek Zawadzki <tomasz.zawadzki@swmansion.com>
@kmagiera kmagiera added this pull request to the merge queue Feb 22, 2023
Merged via the queue into main with commit 63bc57f Feb 22, 2023
@kmagiera kmagiera deleted the fix-testing branch February 22, 2023 14:25
fluiddot pushed a commit to wordpress-mobile/react-native-reanimated that referenced this pull request Jun 5, 2023
## Summary

This PR adds Jest specific code to places where we'd used setImmediate.
We want to avoid calling setImmediate as it makes writing tests a bit
tricky.

The reason is that Jest mocks all the timer-related stuff putting all
enqueued function in a single bucket. Then, in order to run these
enqueued functions, the test needs to call advanceTimersByTime. It isn't
allowed to schedule a callback for the same "tick" which results in the
chain of setImmediate calls requiring a several separate runs to
advanceTimersByTime. This can be seen in the previous workaround where
we had three subsequent calls to that method just in order to start a
single animation.

With this change we no longer expect from test to run any additional
setup / timer-advancing steps when testing reanimated components and
animations.

## Test plan

Run jest tests.

---------

Co-authored-by: Tomek Zawadzki <tomasz.zawadzki@swmansion.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants