Skip to content

Commit c87a2a2

Browse files
test_runner: add ref methods to mocked timers
Fixes: #51701
1 parent 0951e7b commit c87a2a2

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

lib/internal/test_runner/mock/mock_timers.js

+29-1
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
}

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

+34
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)