Skip to content

add jest.advanceTimersToNextTimer method#8713

Merged
SimenB merged 6 commits intojestjs:masterfrom
eranshabi:run-next-timer
Jul 28, 2019
Merged

add jest.advanceTimersToNextTimer method#8713
SimenB merged 6 commits intojestjs:masterfrom
eranshabi:run-next-timer

Conversation

@eranshabi
Copy link
Copy Markdown
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
Copy Markdown
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!

Comment thread packages/jest-environment/src/index.ts
Comment thread packages/jest-fake-timers/src/jestFakeTimers.ts Outdated
Comment thread packages/jest-fake-timers/src/jestFakeTimers.ts Outdated
Comment thread packages/jest-runtime/src/index.ts Outdated
Comment thread packages/jest-fake-timers/src/jestFakeTimers.ts
Comment thread packages/jest-fake-timers/src/__tests__/jestFakeTimers.test.ts
@SimenB SimenB requested review from jeysal and thymikee July 18, 2019 13:35
Comment thread packages/jest-fake-timers/src/__tests__/jestFakeTimers.test.ts Outdated
Copy link
Copy Markdown
Collaborator

@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!

Comment thread packages/jest-fake-timers/src/__tests__/jestFakeTimers.test.ts Outdated
@deanshub
Copy link
Copy Markdown

@thymikee please please please?

@github-actions
Copy link
Copy Markdown

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