Repository navigation
test v1.2.0
Minor Changes
-
t.useFakeTimers()now fakesDate,performance.now(),setImmediate()/clearImmediate(), andAbortSignal.timeout()along with the timer functions, so wall-clock code such as expiry checks and timestamps can be tested without injecting a clock. While fake timers are active,Date.now(),new Date(), andperformance.now()read the fake clock and move forward only withadvance()oradvanceAsync().new Date(value),Date.parse(),Date.UTC(), andinstanceof Datekeep working. Immediates fire on the nextadvance(), andAbortSignal.timeout(ms)signals abort with aTimeoutErroronce the fake clock reachesms. (#12203)Like Node's mock timers, the fake
Datestarts at the Unix epoch (0) andperformance.now()starts at0. Tests that used fake timers while reading the realDate, or while relying on real immediates orAbortSignal.timeout(), now see fake time instead. Pass the newnowoption to start at a specific time:let timers = t.useFakeTimers({ now: new Date('2030-01-01T00:00:00Z') })
-
Added data-property mocking with
mock.property()andt.mock.property(), including read/write records, one-access value overrides, and descriptor restoration. Added getter and setter spies throughmock.getter(),mock.setter(), and the fourthmock.method()argument. ExportedMockTrackeras the shared type for standalone and test-context mocks. (#12225)Test-context accessor and property mocks restore in reverse installation order after cleanup callbacks, even when a test or cleanup callback fails. Method mocks layered over getter spies preserve the accessor's backing value. Method, accessor, and property mocks can be created while
Array.isArrayis mocked, with array argument checks preserved. -
Added public
TestFnandTestMetatypes for reusable test callbacks and metadata, with linked API documentation for the test body and its options. (#12226) -
Add new
t.waitFor(condition, { interval, timeout })API to retry assertions until they pass. Polling stops when the test ends. See Polling Assertions for usage. (#12224)
Patch Changes
-
Fix several gaps in
t.useFakeTimers(): (#12203)clearTimeout()andclearInterval()now cancel real timers created before fake timers were enabled. Previously the fakes ignored those timers, so a test with atimeoutoption that enabled fake timers left its real timeout timer running after it finished, which later aborted the test's signal and kept the process alive.setTimeout()andsetInterval()now pass extra arguments to the callback.- In Node, fake timers return handles with
ref(),unref(),hasRef(), andrefresh(), so code that calls.unref()on a timer no longer throws. Clearing an expired timeout's handle preventsrefresh()from reactivating it. - Calling
t.useFakeTimers()again restores the previous clock before starting a new one, so cleanup restores the originalDate. Repeatedrestore()calls have no effect. - Restore fake timers synchronously on abort and before user cleanup callbacks. Finished or aborted contexts cannot enable another clock, and runner deadlines keep using real timers.
- Reject overlapping clocks, roll back failed installations, and restore the original property descriptors without reinstalling fakes beneath spies. Restoration attempts every property, reports failures, and permits retrying failed properties.
- Keep fake timer IDs unique across clocks so clearing a stale numeric handle cannot cancel a later test's timer.
- Fake
AbortSignal.timeout(ms)validates its argument synchronously using Node's numeric integer range of0through4294967295in every runtime.
-
Fix test and lifecycle hook timeouts stalling under fake timers and prevent late timeouts from aborting completed test signals. (#12224)