Skip to content

Commit cea90dc

Browse files
marco-ippolitorichardlau
authored andcommitted
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 696063a commit cea90dc

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

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

Lines 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+
classTimeout{
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+
returntrue;
81+
}
82+
83+
ref(){
84+
returnthis;
85+
}
86+
87+
unref(){
88+
returnthis;
89+
}
90+
91+
refresh(){
92+
returnthis;
93+
}
94+
}
95+
7096
classMockTimers{
7197
#realSetTimeout;
7298
#realClearTimeout;
@@ -260,14 +286,16 @@ class MockTimers {
260286

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

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

Lines 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+
consttimer=setTimeout();
852+
assert.strictEqual(typeoftimer.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+
consttimer=setTimeout();
860+
assert.ok(typeoftimer.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+
consttimer=setTimeout();
868+
assert.ok(typeoftimer.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+
consttimer=setTimeout();
876+
assert.ok(typeoftimer.refresh==='function');
877+
assert.deepStrictEqual(timer.refresh(),timer);
878+
clearTimeout(timer);
879+
});
880+
});
847881
});

0 commit comments

Comments
 (0)