Skip to content

Commit 205d0a5

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 b97c7be commit 205d0a5

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

1249-
test('runs timers as setTime passes ticks', (context) => {
1249+
test('setTime does not execute timers', (context) => {
12501250
// Optionally choose what to mock
12511251
context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
12521252
const fn = context.mock.fn();
@@ -1258,7 +1258,10 @@ test('runs timers as setTime passes ticks', (context) => {
12581258
assert.strictEqual(Date.now(), 800);
12591259

12601260
context.mock.timers.setTime(1200);
1261-
// Timer is executed as the time is now reached
1261+
// Timer is still not executed
1262+
assert.strictEqual(fn.mock.callCount(), 0);
1263+
// Advance in time to execute the timer
1264+
context.mock.timers.tick(0);
12621265
assert.strictEqual(fn.mock.callCount(), 1);
12631266
assert.strictEqual(Date.now(), 1200);
12641267
});
@@ -2728,8 +2731,8 @@ test('mocks a builtin module in both module systems', async (t) => {
27282731
// cursorTo() is an export of the original 'node:readline' module.
27292732
assert.strictEqual(esmImpl.cursorTo, undefined);
27302733
assert.strictEqual(cjsImpl.cursorTo, undefined);
2731-
assert.strictEqual(esmImpl.fn(), 42);
2732-
assert.strictEqual(cjsImpl.fn(), 42);
2734+
assert.strictEqual(esmImpl.foo(), 42);
2735+
assert.strictEqual(cjsImpl.foo(), 42);
27332736

27342737
mock.restore();
27352738

@@ -2739,8 +2742,8 @@ test('mocks a builtin module in both module systems', async (t) => {
27392742

27402743
assert.strictEqual(typeof esmImpl.cursorTo, 'function');
27412744
assert.strictEqual(typeof cjsImpl.cursorTo, 'function');
2742-
assert.strictEqual(esmImpl.fn, undefined);
2743-
assert.strictEqual(cjsImpl.fn, undefined);
2745+
assert.strictEqual(esmImpl.foo, undefined);
2746+
assert.strictEqual(cjsImpl.foo, undefined);
27442747
});
27452748
```
27462749

0 commit comments

Comments
 (0)