Skip to content

Commit ef42d32

Browse files
authored
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 b09155d commit ef42d32

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
@@ -1248,7 +1248,7 @@ test('setTime does not execute timers', (context) => {
12481248
const assert = require('node:assert');
12491249
const { test } = require('node:test');
12501250

1251-
test('runs timers as setTime passes ticks', (context) => {
1251+
test('setTime does not execute timers', (context) => {
12521252
// Optionally choose what to mock
12531253
context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
12541254
const fn = context.mock.fn();
@@ -1260,7 +1260,10 @@ test('runs timers as setTime passes ticks', (context) => {
12601260
assert.strictEqual(Date.now(), 800);
12611261

12621262
context.mock.timers.setTime(1200);
1263-
// Timer is executed as the time is now reached
1263+
// Timer is still not executed
1264+
assert.strictEqual(fn.mock.callCount(), 0);
1265+
// Advance in time to execute the timer
1266+
context.mock.timers.tick(0);
12641267
assert.strictEqual(fn.mock.callCount(), 1);
12651268
assert.strictEqual(Date.now(), 1200);
12661269
});
@@ -2730,8 +2733,8 @@ test('mocks a builtin module in both module systems', async (t) => {
27302733
// cursorTo() is an export of the original 'node:readline' module.
27312734
assert.strictEqual(esmImpl.cursorTo, undefined);
27322735
assert.strictEqual(cjsImpl.cursorTo, undefined);
2733-
assert.strictEqual(esmImpl.fn(), 42);
2734-
assert.strictEqual(cjsImpl.fn(), 42);
2736+
assert.strictEqual(esmImpl.foo(), 42);
2737+
assert.strictEqual(cjsImpl.foo(), 42);
27352738

27362739
mock.restore();
27372740

@@ -2741,8 +2744,8 @@ test('mocks a builtin module in both module systems', async (t) => {
27412744

27422745
assert.strictEqual(typeof esmImpl.cursorTo, 'function');
27432746
assert.strictEqual(typeof cjsImpl.cursorTo, 'function');
2744-
assert.strictEqual(esmImpl.fn, undefined);
2745-
assert.strictEqual(cjsImpl.fn, undefined);
2747+
assert.strictEqual(esmImpl.foo, undefined);
2748+
assert.strictEqual(cjsImpl.foo, undefined);
27462749
});
27472750
```
27482751

0 commit comments

Comments
 (0)