GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test: add Symbol.dispose support to mock timers · nodejs/node@67925bb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • test.md
  • lib/internal/test_runner/mock
    • mock_timers.js
  • test/parallel
    • test-runner-mock-timers.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/test.md‎

Copy file name to clipboardExpand all lines: doc/api/test.md
+4Lines changed: 4 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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
Collapse file

‎lib/internal/test_runner/mock/mock_timers.js‎

Copy file name to clipboardExpand all lines: lib/internal/test_runner/mock/mock_timers.js
+5Lines changed: 5 additions & 0 deletions
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;
Collapse file

‎test/parallel/test-runner-mock-timers.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-runner-mock-timers.js
+15Lines changed: 15 additions & 0 deletions
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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.