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 <kimaswaemma36@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/63656
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Jacob Smith <jacob@frende.me>
This commit is contained in:
Kimaswa Emmanuel Yusufu authored and Antoine du Hamel committed 2026-07-30 13:51:37 +02:00
1 parent c757e3ef59
commit b7a8f8fabd
1 file changed
+9 -6
+9 -6
View File
@@ -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);
});
```