Skip to content

Conversation

@atscott
Copy link
Contributor

@atscott atscott commented Oct 21, 2025

Summary

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

@netlify
Copy link

netlify bot commented Oct 21, 2025

Deploy Preview for jestjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 5d4bfa6
🔍 Latest deploy log https://app.netlify.com/projects/jestjs/deploys/68fba63849787800071b4a18
😎 Deploy Preview https://deploy-preview-15871--jestjs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 21, 2025

Open in StackBlitz

babel-jest

npm i https://pkg.pr.new/babel-jest@15871

babel-plugin-jest-hoist

npm i https://pkg.pr.new/babel-plugin-jest-hoist@15871

babel-preset-jest

npm i https://pkg.pr.new/babel-preset-jest@15871

create-jest

npm i https://pkg.pr.new/create-jest@15871

@jest/diff-sequences

npm i https://pkg.pr.new/@jest/diff-sequences@15871

expect

npm i https://pkg.pr.new/expect@15871

@jest/expect-utils

npm i https://pkg.pr.new/@jest/expect-utils@15871

jest

npm i https://pkg.pr.new/jest@15871

jest-changed-files

npm i https://pkg.pr.new/jest-changed-files@15871

jest-circus

npm i https://pkg.pr.new/jest-circus@15871

jest-cli

npm i https://pkg.pr.new/jest-cli@15871

jest-config

npm i https://pkg.pr.new/jest-config@15871

@jest/console

npm i https://pkg.pr.new/@jest/console@15871

@jest/core

npm i https://pkg.pr.new/@jest/core@15871

@jest/create-cache-key-function

npm i https://pkg.pr.new/@jest/create-cache-key-function@15871

jest-diff

npm i https://pkg.pr.new/jest-diff@15871

jest-docblock

npm i https://pkg.pr.new/jest-docblock@15871

jest-each

npm i https://pkg.pr.new/jest-each@15871

@jest/environment

npm i https://pkg.pr.new/@jest/environment@15871

jest-environment-jsdom

npm i https://pkg.pr.new/jest-environment-jsdom@15871

@jest/environment-jsdom-abstract

npm i https://pkg.pr.new/@jest/environment-jsdom-abstract@15871

jest-environment-node

npm i https://pkg.pr.new/jest-environment-node@15871

@jest/expect

npm i https://pkg.pr.new/@jest/expect@15871

@jest/fake-timers

npm i https://pkg.pr.new/@jest/fake-timers@15871

@jest/get-type

npm i https://pkg.pr.new/@jest/get-type@15871

@jest/globals

npm i https://pkg.pr.new/@jest/globals@15871

jest-haste-map

npm i https://pkg.pr.new/jest-haste-map@15871

jest-jasmine2

npm i https://pkg.pr.new/jest-jasmine2@15871

jest-leak-detector

npm i https://pkg.pr.new/jest-leak-detector@15871

jest-matcher-utils

npm i https://pkg.pr.new/jest-matcher-utils@15871

jest-message-util

npm i https://pkg.pr.new/jest-message-util@15871

jest-mock

npm i https://pkg.pr.new/jest-mock@15871

@jest/pattern

npm i https://pkg.pr.new/@jest/pattern@15871

jest-phabricator

npm i https://pkg.pr.new/jest-phabricator@15871

jest-regex-util

npm i https://pkg.pr.new/jest-regex-util@15871

@jest/reporters

npm i https://pkg.pr.new/@jest/reporters@15871

jest-resolve

npm i https://pkg.pr.new/jest-resolve@15871

jest-resolve-dependencies

npm i https://pkg.pr.new/jest-resolve-dependencies@15871

jest-runner

npm i https://pkg.pr.new/jest-runner@15871

jest-runtime

npm i https://pkg.pr.new/jest-runtime@15871

@jest/schemas

npm i https://pkg.pr.new/@jest/schemas@15871

jest-snapshot

npm i https://pkg.pr.new/jest-snapshot@15871

@jest/snapshot-utils

npm i https://pkg.pr.new/@jest/snapshot-utils@15871

@jest/source-map

npm i https://pkg.pr.new/@jest/source-map@15871

@jest/test-result

npm i https://pkg.pr.new/@jest/test-result@15871

@jest/test-sequencer

npm i https://pkg.pr.new/@jest/test-sequencer@15871

@jest/transform

npm i https://pkg.pr.new/@jest/transform@15871

@jest/types

npm i https://pkg.pr.new/@jest/types@15871

jest-util

npm i https://pkg.pr.new/jest-util@15871

jest-validate

npm i https://pkg.pr.new/jest-validate@15871

jest-watcher

npm i https://pkg.pr.new/jest-watcher@15871

jest-worker

npm i https://pkg.pr.new/jest-worker@15871

pretty-format

npm i https://pkg.pr.new/pretty-format@15871

commit: 5d4bfa6

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.

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

@atscott atscott force-pushed the v15 branch 4 times, most recently from 5b1f84e to 3373a48 Compare October 23, 2025 17:40
@atscott atscott requested a review from SimenB October 23, 2025 21:22
@atscott atscott force-pushed the v15 branch 4 times, most recently from 702cc55 to 9097622 Compare October 24, 2025 15:59
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
@atscott
Copy link
Contributor Author

atscott commented Oct 24, 2025

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

@SimenB
Copy link
Member

SimenB commented Oct 27, 2025

It's fine 🙂 Not sure why -0.0.3% bright red 😅 But no, e2e is not counted towards test coverage percentage

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.

wonderful stuff, thanks! Love the upstream DT fixes as well 👏

@SimenB SimenB merged commit 6813dfa into jestjs:main Oct 27, 2025
72 of 74 checks passed
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.

2 participants