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

add jest.advanceTimersToNextTimer method #8713

Merged
merged 6 commits into from
Jul 28, 2019

Conversation

eranshabi
Copy link
Contributor

A contribution by me and @deanshub

Summary

When you test a code (using fake timers) with lots of timeouts/intervals, and some of the code is 3rd party, it's hard to keep track of the right amount of milliseconds you should advance timers by.
Moreover, sometimes you don't want to run all the timers, because you might want to expect something before the end of them all. (Maybe you need to assert between the second and third timeouts)
jest.advanceTimersToNextTimer will advance the timers by the correct milliseconds that make sense for the next assertion.

The alternative is to run jest.advanceTimersByTime(1) a lot of times until you ran the right amount to make your test pass (runAllTimers will run too much timers).
We tried this new feature on our tests and they look much cleaner.

Test plan

Added tests to jest-fake-timers

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, thank you so much for sending a PR!

packages/jest-environment/src/index.ts Show resolved Hide resolved
packages/jest-fake-timers/src/jestFakeTimers.ts Outdated Show resolved Hide resolved
packages/jest-fake-timers/src/jestFakeTimers.ts Outdated Show resolved Hide resolved
packages/jest-runtime/src/index.ts Outdated Show resolved Hide resolved
@SimenB SimenB requested review from thymikee and jeysal July 18, 2019 13:35
Copy link
Contributor

@jeysal jeysal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks solid, thanks!

@deanshub
Copy link

@thymikee please please please?

@github-actions
Copy link

This pull request 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 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants