Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 50 additions & 26 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function abortIt(signal) {
return new AbortError(undefined, { cause: signal.reason });
}

const SUPPORTED_TIMERS = ['setTimeout', 'setInterval'];
const SUPPORTED_TIMERS = ['setTimeout', 'setInterval', 'Date.now'];

class MockTimers {
#realSetTimeout;
Expand All @@ -64,6 +64,8 @@ class MockTimers {
#realTimersSetInterval;
#realTimersClearInterval;

#realDateNow;

#timersInContext = [];
#isEnabled = false;
#currentTimer = 1;
Expand All @@ -75,6 +77,7 @@ class MockTimers {
#clearTimeout = FunctionPrototypeBind(this.#clearTimer, this);
#setInterval = FunctionPrototypeBind(this.#createTimer, this, true);
#clearInterval = FunctionPrototypeBind(this.#clearTimer, this);
#dateNow = FunctionPrototypeBind(this.#fakeDateNow, this);

constructor() {
emitExperimentalWarning('The MockTimers API');
Expand All @@ -98,7 +101,11 @@ class MockTimers {
this.#executionQueue.removeAt(position);
}

async * #setIntervalPromisified(interval, startTime, options) {
#fakeDateNow() {
return this.#now;
}

async *#setIntervalPromisified(interval, startTime, options) {
const context = this;
const emitter = new EventEmitter();
if (options?.signal) {
Expand All @@ -112,7 +119,8 @@ class MockTimers {
emitter.emit('data', { __proto__: null, aborted: true, reason });
};

kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
kResistStopPropagation ??=
require('internal/event_target').kResistStopPropagation;
options.signal.addEventListener('abort', onAbort, {
__proto__: null,
once: true,
Expand Down Expand Up @@ -182,7 +190,8 @@ class MockTimers {
}, ms);

if (options?.signal) {
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
kResistStopPropagation ??=
require('internal/event_target').kResistStopPropagation;
options.signal.addEventListener('abort', onabort, {
__proto__: null,
once: true,
Expand All @@ -192,6 +201,20 @@ class MockTimers {
});
}

#assertTimersAreEnabled() {
if (!this.#isEnabled) {
throw new ERR_INVALID_STATE(
'You should enable MockTimers first by calling the .enable function'
);
}
}

#assertTimeArg(time) {
if (time < 0) {
throw new ERR_INVALID_ARG_VALUE('time', 'positive integer', time);
}
}

#toggleEnableTimers(activate) {
const options = {
toFake: {
Expand All @@ -210,7 +233,7 @@ class MockTimers {

nodeTimersPromises.setTimeout = FunctionPrototypeBind(
this.#setTimeoutPromisified,
this,
this
);
},
setInterval: () => {
Expand All @@ -228,9 +251,13 @@ class MockTimers {

nodeTimersPromises.setInterval = FunctionPrototypeBind(
this.#setIntervalPromisified,
this,
this
);
},
'Date.now': () => {
this.#realDateNow = globalThis.Date.now;
globalThis.Date.now = this.#dateNow;
},
},
toReal: {
setTimeout: () => {
Expand All @@ -251,6 +278,9 @@ class MockTimers {

nodeTimersPromises.setInterval = this.#realPromisifiedSetInterval;
},
'Date.now': () => {
globalThis.Date.now = this.#realDateNow;
},
},
};

Expand All @@ -260,19 +290,8 @@ class MockTimers {
}

tick(time = 1) {
if (!this.#isEnabled) {
throw new ERR_INVALID_STATE(
'You should enable MockTimers first by calling the .enable function',
);
}

if (time < 0) {
throw new ERR_INVALID_ARG_VALUE(
'time',
'positive integer',
time,
);
}
this.#assertTimersAreEnabled();
this.#assertTimeArg(time);

this.#now += time;
let timer = this.#executionQueue.peek();
Expand All @@ -292,11 +311,9 @@ class MockTimers {
}
}

enable(timers = SUPPORTED_TIMERS) {
enable(timers = SUPPORTED_TIMERS, currentTime = DateNow()) {
if (this.#isEnabled) {
throw new ERR_INVALID_STATE(
'MockTimers is already enabled!',
);
throw new ERR_INVALID_STATE('MockTimers is already enabled!');
}

validateArray(timers, 'timers');
Expand All @@ -307,16 +324,22 @@ class MockTimers {
throw new ERR_INVALID_ARG_VALUE(
'timers',
timer,
`option ${timer} is not supported`,
`option ${timer} is not supported`
);
}
});

this.#timersInContext = timers;
this.#now = DateNow();
this.#now = currentTime;
this.#toggleEnableTimers(true);
}

setTime(time = DateNow()) {
this.#assertTimeArg(time);
this.#assertTimersAreEnabled();
this.#now = time;
}

[SymbolDispose]() {
this.reset();
}
Expand All @@ -327,6 +350,7 @@ class MockTimers {

this.#toggleEnableTimers(false);
this.#timersInContext = [];
this.#now = DateNow();

let timer = this.#executionQueue.peek();
while (timer) {
Expand All @@ -338,7 +362,7 @@ class MockTimers {
runAll() {
if (!this.#isEnabled) {
throw new ERR_INVALID_STATE(
'You should enable MockTimers first by calling the .enable function',
'You should enable MockTimers first by calling the .enable function'
);
}

Expand Down