Skip to content

Commit c8c42f8

Browse files
committed
test: add Symbol.dispose support to mock timers
PR-URL: nodejs/node#48549 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Erick Wendel <erick.workspace@gmail.com>
1 parent 24a4b63 commit c8c42f8

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

graal-nodejs/doc/api/test.md

+4
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,10 @@ const { mock } = require('node:test');
15871587
mock.timers.reset();
15881588
```
15891589

1590+
### `timers[Symbol.dispose]()`
1591+
1592+
Calls `timers.reset()`.
1593+
15901594
### `timers.tick(milliseconds)`
15911595

15921596
<!-- YAML

graal-nodejs/lib/internal/test_runner/mock/mock_timers.js

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
FunctionPrototypeBind,
1313
Promise,
1414
SymbolAsyncIterator,
15+
SymbolDispose,
1516
globalThis,
1617
} = primordials;
1718
const {
@@ -315,6 +316,10 @@ class MockTimers {
315316
this.#toggleEnableTimers(true);
316317
}
317318

319+
[SymbolDispose]() {
320+
this.reset();
321+
}
322+
318323
reset() {
319324
// Ignore if not enabled
320325
if (!this.#isEnabled) return;

graal-nodejs/test/parallel/test-runner-mock-timers.js

+15
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ describe('Mock Timers Test Suite', () => {
6161
assert.strictEqual(fn.mock.callCount(), 0);
6262
});
6363

64+
it('should reset all timers when calling Symbol.dispose', (t) => {
65+
t.mock.timers.enable();
66+
const fn = t.mock.fn();
67+
global.setTimeout(fn, 1000);
68+
// TODO(benjamingr) refactor to `using`
69+
t.mock.timers[Symbol.dispose]();
70+
assert.throws(() => {
71+
t.mock.timers.tick(1000);
72+
}, {
73+
code: 'ERR_INVALID_STATE',
74+
});
75+
76+
assert.strictEqual(fn.mock.callCount(), 0);
77+
});
78+
6479
it('should execute in order if timeout is the same', (t) => {
6580
t.mock.timers.enable();
6681
const order = [];

0 commit comments

Comments
 (0)