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_runner: align mock timeout api · nodejs/node@d142c58

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • 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

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

Copy file name to clipboardExpand all lines: lib/internal/test_runner/mock/mock_timers.js
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,15 @@ const TIMERS_DEFAULT_INTERVAL = {
7979
};
8080

8181
class Timeout {
82+
#clear;
83+
8284
constructor(opts) {
8385
this.id = opts.id;
8486
this.callback = opts.callback;
8587
this.runAt = opts.runAt;
8688
this.interval = opts.interval;
8789
this.args = opts.args;
90+
this.#clear = opts.clear;
8891
}
8992

9093
hasRef() {
@@ -102,6 +105,15 @@ class Timeout {
102105
refresh() {
103106
return this;
104107
}
108+
109+
close() {
110+
this.#clear(this);
111+
return this;
112+
}
113+
114+
[SymbolDispose]() {
115+
this.#clear(this);
116+
}
105117
}
106118

107119
class MockTimers {
@@ -329,6 +341,7 @@ class MockTimers {
329341
runAt: this.#now + delay,
330342
interval: isInterval ? delay : undefined,
331343
args,
344+
clear: this.#clearTimeout,
332345
};
333346

334347
const timer = new Timeout(opts);
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-runner-mock-timers.js
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,32 @@ describe('Mock Timers Test Suite', () => {
266266
assert.deepStrictEqual(fn.mock.calls[0].arguments, args);
267267
});
268268

269+
it('should expose Timeout.prototype[Symbol.dispose]', (t) => {
270+
t.mock.timers.enable({ apis: ['setTimeout'] });
271+
const fn = t.mock.fn();
272+
const timeout = globalThis.setTimeout(fn, 2000);
273+
274+
assert.strictEqual(typeof timeout[Symbol.dispose], 'function');
275+
276+
timeout[Symbol.dispose]();
277+
t.mock.timers.tick(2000);
278+
279+
assert.strictEqual(fn.mock.callCount(), 0);
280+
});
281+
282+
it('should expose Timeout.prototype.close()', (t) => {
283+
t.mock.timers.enable({ apis: ['setTimeout'] });
284+
const fn = t.mock.fn();
285+
const timeout = globalThis.setTimeout(fn, 2000);
286+
287+
assert.strictEqual(typeof timeout.close, 'function');
288+
assert.strictEqual(timeout.close(), timeout);
289+
290+
t.mock.timers.tick(2000);
291+
292+
assert.strictEqual(fn.mock.callCount(), 0);
293+
});
294+
269295
it('should keep setTimeout working if timers are disabled', (t, done) => {
270296
const now = Date.now();
271297
const timeout = 2;

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.