From b7a8f8fabddcf94152bf27eee3e565d3be7830fc Mon Sep 17 00:00:00 2001 From: Kimaswa Emmanuel Yusufu <67486402+emmayusufu@users.noreply.github.com> Date: Wed, 17 Jun 2026 12:11:36 +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 PR-URL: https://github.com/nodejs/node/pull/63656 Reviewed-By: Chemi Atlow Reviewed-By: Jacob Smith --- 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 96178f2a565..4967f7b265d 100644 --- a/doc/api/test.md +++ b/doc/api/test.md @@ -1168,7 +1168,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(); @@ -1180,7 +1180,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); }); @@ -2631,8 +2634,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(); @@ -2642,8 +2645,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); }); ```