-
-
Couldn't load subscription status.
- Fork 6.6k
feat(jest-fake-timers): Add feature to configure how timers advance #15871
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
Conversation
✅ Deploy Preview for jestjs ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
babel-jest
babel-plugin-jest-hoist
babel-preset-jest
create-jest
@jest/diff-sequences
expect
@jest/expect-utils
jest
jest-changed-files
jest-circus
jest-cli
jest-config
@jest/console
@jest/core
@jest/create-cache-key-function
jest-diff
jest-docblock
jest-each
@jest/environment
jest-environment-jsdom
@jest/environment-jsdom-abstract
jest-environment-node
@jest/expect
@jest/fake-timers
@jest/get-type
@jest/globals
jest-haste-map
jest-jasmine2
jest-leak-detector
jest-matcher-utils
jest-message-util
jest-mock
@jest/pattern
jest-phabricator
jest-regex-util
@jest/reporters
jest-resolve
jest-resolve-dependencies
jest-runner
jest-runtime
@jest/schemas
jest-snapshot
@jest/snapshot-utils
@jest/source-map
@jest/test-result
@jest/test-sequencer
@jest/transform
@jest/types
jest-util
jest-validate
jest-watcher
jest-worker
pretty-format
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
happy to land this 🙂
Should update the docs as well: https://github.com/jestjs/jest/blob/9a3ec0f5311325c3abb07cc942d26bc934e3b6c3/docs/JestObjectAPI.md#fake-timers
also, please fix the linting errors
5b1f84e to
3373a48
Compare
702cc55 to
9097622
Compare
this updates to v15 and adds patches to the timers.
This change exposes the `setTimerTickMode` function from the underlying `fake-timers` library.
This is to align with the new feature introduced in `fake-timers`.
The new `setTimerTickMode` method allows developers to configure whether time should auto advance and what the delta should be after the clock has been created. Prior to this, it could only be done at creation time with `shouldAdvanceTime: true`.
This also adds a new mode for automatically advancing time that moves more quickly than the existing shouldAdvanceTime, which uses real time.
Testing with mock clocks can often turn into a real struggle when dealing with situations where some work in the test is truly async and other work is captured by the mock clock. When using mock clocks, testers are always forced to write tests with intimate knowledge of when the mock clock needs to be ticked. It is ideal for test code to be written in a way that is independent of whether a mock clock is installed.
`shouldAdvanceTime` is essentially `setInterval(() => clock.tick(ms), ms)` while this feature is `const loop = () => setTimeout(() => clock.advancetimerstoNextTimerAsync().then(() => loop()), 0);`
There are two key differences:
1. `shouldAdvanceTime` uses `clock.tick(ms)` so it synchronously runs all timers inside the "ms" of the clock queue. This doesn't allow the microtask queue to empty between the macrotask timers in the clock.
2. `shouldAdvanceTime` uses real time to advance the same amount of real time in the mock clock. `setTimerTickMode({mode: "nextAsync"})` advances time as quickly possible and as far as necessary.
See: sinonjs/fake-timers@108efae
|
@SimenB I'm having difficulty with the codecov/patch failure. I thought adding an e2e test would make the coverage pass but it didn't. Where would I need to add a test for codecov to work there? |
|
It's fine 🙂 Not sure why |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wonderful stuff, thanks! Love the upstream DT fixes as well 👏
Summary
This change exposes the
setTimerTickModefunction from the underlyingfake-timerslibrary.This is to align with the new feature introduced in
fake-timers.The new
setTimerTickModemethod allows developers to configure whether time should auto advance and what the delta should be after the clock has been created. Prior to this, it could only be done at creation time withshouldAdvanceTime: true.This also adds a new mode for automatically advancing time that moves more quickly than the existing shouldAdvanceTime, which uses real time.
Testing with mock clocks can often turn into a real struggle when dealing with situations where some work in the test is truly async and other work is captured by the mock clock. When using mock clocks, testers are always forced to write tests with intimate knowledge of when the mock clock needs to be ticked. It is ideal for test code to be written in a way that is independent of whether a mock clock is installed.
shouldAdvanceTimeis essentiallysetInterval(() => clock.tick(ms), ms)while this feature isconst loop = () => setTimeout(() => clock.advancetimerstoNextTimerAsync().then(() => loop()), 0);There are two key differences:
shouldAdvanceTimeusesclock.tick(ms)so it synchronously runs all timers inside the "ms" of the clock queue. This doesn't allow the microtask queue to empty between the macrotask timers in the clock.shouldAdvanceTimeuses real time to advance the same amount of real time in the mock clock.setTimerTickMode({mode: "nextAsync"})advances time as quickly possible and as far as necessary.See: sinonjs/fake-timers@108efae