fix(testing): FakeTime fakes node:timers and node:timers/promises timer methods#7181
Open
truffle-dev wants to merge 2 commits into
Open
fix(testing): FakeTime fakes node:timers and node:timers/promises timer methods#7181truffle-dev wants to merge 2 commits into
FakeTime fakes node:timers and node:timers/promises timer methods#7181truffle-dev wants to merge 2 commits into
Conversation
…s` timer methods
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7181 +/- ##
==========================================
- Coverage 94.57% 94.57% -0.01%
==========================================
Files 636 637 +1
Lines 52142 52200 +58
Branches 9401 9406 +5
==========================================
+ Hits 49315 49367 +52
- Misses 2249 2254 +5
- Partials 578 579 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…ithout getBuiltinModule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7177.
FakeTimeonly swaps theglobalThistimer functions (plusDateandAbortSignal.timeout), but thenode:timerspolyfill never reads those globals —timers.promises.setTimeoutconstructs aTimeoutthat schedules through the internalcreateTimer_primitive, so the fake clock is invisible to anything entering throughnode:timers. The repro in the issue hangs because the real one-second timer never fires inside the test's fake-time window.This patch makes
overrideGlobals()/restoreGlobals()also swap the timer methods on thenode:timersandnode:timers/promisesmodule objects:setTimeout/clearTimeout/setInterval/clearIntervaldelegate to the same fakes already used for the globals, andnode:timers/promisessetTimeoutgets a promise fake built on the fake scheduler that honorssignal(both pre-aborted and aborted mid-flight). In Deno the object attached attimers.promisesand thenode:timers/promisesmodule object are distinct, so both are patched.Two implementation notes:
process.getBuiltinModule()rather than staticnode:imports. A static import adds anode:edge to the published module graph, which_tools/check_docs.ts(via@deno/doc) fails to load;getBuiltinModulereturns the same module object (the new tests compare against statically imported references), is synchronous, and lets runtimes without the Node compatibility layer skip the patching instead of failing to load@std/testing/time. Deno 1.x predatesgetBuiltinModule, so the faking and the corresponding tests are skipped there (verified locally against 1.46.3); the test file also views the module objects through local types, since the Node typings bundled with Deno 1.x lackpromisesonnode:timersand declare an incompatibleAbortSignalonnode:timers/promises.import { setTimeout } from "node:timers/promises"capture the real implementation at module load and cannot be faked afterwards. This is documented in theFakeTimejsdoc along with a runnable example.setImmediateand the promise-basedsetIntervalare left unfaked, consistent withFakeTimenot faking immediates onglobalThistoday; the jsdoc lists exactly which methods are faked.The issue's repro shape is covered in
time_test.ts, plus faked/restored identity checks mirroring the existing global-timer tests and abort rejection coverage for the promise fake.deno task lint,deno fmt --check, anddeno test -A --doc testing/all pass locally.