Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5f94912

Browse filesBrowse files
test_runner: add ref methods to mocked timers
Fixes: #51701 PR-URL: #51809 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Tierney Cyren <hello@bnb.im>
1 parent 8f10543 commit 5f94912
Copy full SHA for 5f94912

File tree

Expand file treeCollapse file tree

2 files changed

+63
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+63
-1
lines changed
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
+29-1Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,32 @@ const TIMERS_DEFAULT_INTERVAL = {
6767
setImmediate: -1,
6868
};
6969

70+
class Timeout {
71+
constructor(opts) {
72+
this.id = opts.id;
73+
this.callback = opts.callback;
74+
this.runAt = opts.runAt;
75+
this.interval = opts.interval;
76+
this.args = opts.args;
77+
}
78+
79+
hasRef() {
80+
return true;
81+
}
82+
83+
ref() {
84+
return this;
85+
}
86+
87+
unref() {
88+
return this;
89+
}
90+
91+
refresh() {
92+
return this;
93+
}
94+
}
95+
7096
class MockTimers {
7197
#realSetTimeout;
7298
#realClearTimeout;
@@ -260,14 +286,16 @@ class MockTimers {
260286

261287
#createTimer(isInterval, callback, delay, ...args) {
262288
const timerId = this.#currentTimer++;
263-
const timer = {
289+
const opts = {
264290
__proto__: null,
265291
id: timerId,
266292
callback,
267293
runAt: this.#now + delay,
268294
interval: isInterval ? delay : undefined,
269295
args,
270296
};
297+
298+
const timer = new Timeout(opts);
271299
this.#executionQueue.insert(timer);
272300
return timer;
273301
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-runner-mock-timers.js
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,4 +844,38 @@ describe('Mock Timers Test Suite', () => {
844844
clearTimeout(id);
845845
});
846846
});
847+
848+
describe('Api should have same public properties as original', () => {
849+
it('should have hasRef', (t) => {
850+
t.mock.timers.enable();
851+
const timer = setTimeout();
852+
assert.strictEqual(typeof timer.hasRef, 'function');
853+
assert.strictEqual(timer.hasRef(), true);
854+
clearTimeout(timer);
855+
});
856+
857+
it('should have ref', (t) => {
858+
t.mock.timers.enable();
859+
const timer = setTimeout();
860+
assert.ok(typeof timer.ref === 'function');
861+
assert.deepStrictEqual(timer.ref(), timer);
862+
clearTimeout(timer);
863+
});
864+
865+
it('should have unref', (t) => {
866+
t.mock.timers.enable();
867+
const timer = setTimeout();
868+
assert.ok(typeof timer.unref === 'function');
869+
assert.deepStrictEqual(timer.unref(), timer);
870+
clearTimeout(timer);
871+
});
872+
873+
it('should have refresh', (t) => {
874+
t.mock.timers.enable();
875+
const timer = setTimeout();
876+
assert.ok(typeof timer.refresh === 'function');
877+
assert.deepStrictEqual(timer.refresh(), timer);
878+
clearTimeout(timer);
879+
});
880+
});
847881
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.