Skip to content

Commit 6723b1e

Browse files
emmayusufuaduh95
authored andcommitted
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: #63656 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent 1d62b24 commit 6723b1e

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

doc/api/test.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ test('setTime does not execute timers', (context) => {
11681168
const assert = require('node:assert');
11691169
const { test } = require('node:test');
11701170

1171-
test('runs timers as setTime passes ticks', (context) => {
1171+
test('setTime does not execute timers', (context) => {
11721172
// Optionally choose what to mock
11731173
context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
11741174
const fn = context.mock.fn();
@@ -1180,7 +1180,10 @@ test('runs timers as setTime passes ticks', (context) => {
11801180
assert.strictEqual(Date.now(), 800);
11811181

11821182
context.mock.timers.setTime(1200);
1183-
// Timer is executed as the time is now reached
1183+
// Timer is still not executed
1184+
assert.strictEqual(fn.mock.callCount(), 0);
1185+
// Advance in time to execute the timer
1186+
context.mock.timers.tick(0);
11841187
assert.strictEqual(fn.mock.callCount(), 1);
11851188
assert.strictEqual(Date.now(), 1200);
11861189
});
@@ -2631,8 +2634,8 @@ test('mocks a builtin module in both module systems', async (t) => {
26312634
// cursorTo() is an export of the original 'node:readline' module.
26322635
assert.strictEqual(esmImpl.cursorTo, undefined);
26332636
assert.strictEqual(cjsImpl.cursorTo, undefined);
2634-
assert.strictEqual(esmImpl.fn(), 42);
2635-
assert.strictEqual(cjsImpl.fn(), 42);
2637+
assert.strictEqual(esmImpl.foo(), 42);
2638+
assert.strictEqual(cjsImpl.foo(), 42);
26362639

26372640
mock.restore();
26382641

@@ -2642,8 +2645,8 @@ test('mocks a builtin module in both module systems', async (t) => {
26422645

26432646
assert.strictEqual(typeof esmImpl.cursorTo, 'function');
26442647
assert.strictEqual(typeof cjsImpl.cursorTo, 'function');
2645-
assert.strictEqual(esmImpl.fn, undefined);
2646-
assert.strictEqual(cjsImpl.fn, undefined);
2648+
assert.strictEqual(esmImpl.foo, undefined);
2649+
assert.strictEqual(cjsImpl.foo, undefined);
26472650
});
26482651
```
26492652

0 commit comments

Comments
 (0)