Skip to content

Commit 038cb75

Browse files
authored
doc: fix test settime docs
PR-URL: #61117 Reviewed-By: Aviv Keller <[email protected]> Reviewed-By: Gürgün Dayıoğlu <[email protected]> Reviewed-By: Xuguang Mei <[email protected]>
1 parent dc97b50 commit 038cb75

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

doc/api/test.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ example of a state file:
175175
```json
176176
[
177177
{
178-
"test.js:10:5": { "passed_on_attempt": 0, "name": "test 1" },
178+
"test.js:10:5": { "passed_on_attempt": 0, "name": "test 1" }
179179
},
180180
{
181181
"test.js:10:5": { "passed_on_attempt": 0, "name": "test 1" },
@@ -915,7 +915,7 @@ test('mocks the Date object with initial time', (context) => {
915915
You can use the `.setTime()` method to manually move the mocked date to another
916916
time. This method only accepts a positive integer.
917917

918-
**Note:** This method will execute any mocked timers that are in the past
918+
**Note:** This method will **not** execute any mocked timers that are in the past
919919
from the new time.
920920

921921
In the below example we are setting a new time for the mocked date.
@@ -952,15 +952,14 @@ test('sets the time of a date object', (context) => {
952952
});
953953
```
954954

955-
If you have any timer that's set to run in the past, it will be executed as if
956-
the `.tick()` method has been called. This is useful if you want to test
957-
time-dependent functionality that's already in the past.
955+
Timers scheduled in the past will **not** run when you call `setTime()`. To execute those timers, you can use
956+
the `.tick()` method to move forward from the new time.
958957

959958
```mjs
960959
import assert from 'node:assert';
961960
import { test } from 'node:test';
962961

963-
test('runs timers as setTime passes ticks', (context) => {
962+
test('setTime does not execute timers', (context) => {
964963
// Optionally choose what to mock
965964
context.mock.timers.enable({ apis: ['setTimeout', 'Date'] });
966965
const fn = context.mock.fn();
@@ -972,7 +971,10 @@ test('runs timers as setTime passes ticks', (context) => {
972971
assert.strictEqual(Date.now(), 800);
973972

974973
context.mock.timers.setTime(1200);
975-
// Timer is executed as the time is now reached
974+
// Timer is still not executed
975+
assert.strictEqual(fn.mock.callCount(), 0);
976+
// Advance in time to execute the timer
977+
context.mock.timers.tick(0);
976978
assert.strictEqual(fn.mock.callCount(), 1);
977979
assert.strictEqual(Date.now(), 1200);
978980
});
@@ -3068,7 +3070,7 @@ Dates and timer objects are dependent on each other. If you use `setTime()` to
30683070
pass the current time to the mocked `Date` object, the set timers with
30693071
`setTimeout` and `setInterval` will **not** be affected.
30703072

3071-
However, the `tick` method **will** advanced the mocked `Date` object.
3073+
However, the `tick` method **will** advance the mocked `Date` object.
30723074

30733075
```mjs
30743076
import assert from 'node:assert';

0 commit comments

Comments
 (0)