The new feature allows developers to write more reliable and predictable tests for time-dependent functionality.
It includes MockTimers with the ability to mock setTimeout, setInterval from globals, node:timers, and node:timers/promises.
The feature provides a simple API to advance time, enable specific timers, and release all timers.
importassertfrom'node:assert';import{test}from'node:test';test('mocks setTimeout to be executed synchronously without having to actually wait for it',(context)=>{constfn=context.mock.fn();// Optionally choose what to mockcontext.mock.timers.enable(['setTimeout']);constnineSecs=9000;setTimeout(fn,nineSecs);constthreeSeconds=3000;context.mock.timers.tick(threeSeconds);context.mock.timers.tick(threeSeconds);context.mock.timers.tick(threeSeconds);assert.strictEqual(fn.mock.callCount(),1);});
This feature was contributed by Erick Wendel in #47775.
Support to the explicit resource management proposal
Node is adding support to the explicit resource management
proposal to its resources allowing users of TypeScript/babel to use using/await using with
V8 support for everyone else on the way.
This feature was contributed by Moshe Atlow and Benjamin Gruenbaum in #48518.
Here is everything you need to know about this upgrade. Please take a good look at what changed and the test results before merging this pull request.
What changed?
Release Notes
20.4.0
All Depfu comment commands