From 786408943854cff7422b9113736a00ef3d052379 Mon Sep 17 00:00:00 2001 From: Emmanuel Yusufu Kimaswa Date: Sat, 30 May 2026 09:39:30 +0300 Subject: [PATCH] doc: fix incorrect test runner mock examples The CJS mock.timers.setTime example asserted the timer ran right after setTime() with no tick() call, which contradicts the documented behavior and the ESM example above it. The mock.module example called .fn() on an export named foo. Both threw if run. Signed-off-by: Emmanuel Yusufu Kimaswa --- doc/api/test.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/doc/api/test.md b/doc/api/test.md index 2d38f136f28332..ca200a50e9ecc8 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -1248,7 +1248,7 @@ test('setTime does not execute timers', (context) => { const assert = require('node:assert'); const { test } = require('node:test'); -test('runs timers as setTime passes ticks', (context) => { +test('setTime does not execute timers', (context) => { // Optionally choose what to mock context.mock.timers.enable({ apis: ['setTimeout', 'Date'] }); const fn = context.mock.fn(); @@ -1260,7 +1260,10 @@ test('runs timers as setTime passes ticks', (context) => { assert.strictEqual(Date.now(), 800); context.mock.timers.setTime(1200); - // Timer is executed as the time is now reached + // Timer is still not executed + assert.strictEqual(fn.mock.callCount(), 0); + // Advance in time to execute the timer + context.mock.timers.tick(0); assert.strictEqual(fn.mock.callCount(), 1); assert.strictEqual(Date.now(), 1200); }); @@ -2730,8 +2733,8 @@ test('mocks a builtin module in both module systems', async (t) => { // cursorTo() is an export of the original 'node:readline' module. assert.strictEqual(esmImpl.cursorTo, undefined); assert.strictEqual(cjsImpl.cursorTo, undefined); - assert.strictEqual(esmImpl.fn(), 42); - assert.strictEqual(cjsImpl.fn(), 42); + assert.strictEqual(esmImpl.foo(), 42); + assert.strictEqual(cjsImpl.foo(), 42); mock.restore(); @@ -2741,8 +2744,8 @@ test('mocks a builtin module in both module systems', async (t) => { assert.strictEqual(typeof esmImpl.cursorTo, 'function'); assert.strictEqual(typeof cjsImpl.cursorTo, 'function'); - assert.strictEqual(esmImpl.fn, undefined); - assert.strictEqual(cjsImpl.fn, undefined); + assert.strictEqual(esmImpl.foo, undefined); + assert.strictEqual(cjsImpl.foo, undefined); }); ```