Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: add support for mocking setImmediate timer #49397

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -1579,9 +1579,10 @@ added:
Enables timer mocking for the specified timers.

* `timers` {Array} An optional array containing the timers to mock.
The currently supported timer values are `'setInterval'` and `'setTimeout'`.
If no array is provided, all timers (`'setInterval'`, `'clearInterval'`, `'setTimeout'`,
and `'clearTimeout'`) will be mocked by default.
The currently supported timer values are `'setInterval'`, `'setTimeout'`
atlowChemi marked this conversation as resolved.
Show resolved Hide resolved
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved
and `'setImmediate'`. If no array is provided, all timers (`'setInterval'`,
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved
`'clearInterval'`, `'setTimeout'`, `'clearTimeout'`, `'setImmediate'`,
and `'clearImmediate'`) will be mocked by default.
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved

**Note:** When you enable mocking for a specific timer, its associated
clear function will also be implicitly mocked.
Expand Down
78 changes: 73 additions & 5 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@ function abortIt(signal) {
return new AbortError(undefined, { __proto__: null, cause: signal.reason });
}

const SUPPORTED_TIMERS = ['setTimeout', 'setInterval'];
const SUPPORTED_TIMERS = ['setTimeout', 'setInterval', 'setImmediate'];
const TIMERS_DEFAULT_INTERVAL = {
__proto__: null,
setImmediate: -1,
nextTick: -2,
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved
};

class MockTimers {
#realSetTimeout;
#realClearTimeout;
#realSetInterval;
#realClearInterval;
#realSetImmediate;
#realClearImmediate;

#realPromisifiedSetTimeout;
#realPromisifiedSetInterval;
Expand All @@ -63,6 +70,9 @@ class MockTimers {
#realTimersClearTimeout;
#realTimersSetInterval;
#realTimersClearInterval;
#realTimersSetImmediate;
#realTimersClearImmediate;
#realPromisifiedSetImmediate;

#timersInContext = [];
#isEnabled = false;
Expand All @@ -76,6 +86,16 @@ class MockTimers {
#setInterval = FunctionPrototypeBind(this.#createTimer, this, true);
#clearInterval = FunctionPrototypeBind(this.#clearTimer, this);

#setImmediate = (callback, ...args) => {
return this.#createTimer(
false,
callback,
TIMERS_DEFAULT_INTERVAL.setImmediate,
...args,
);
};

#clearImmediate = FunctionPrototypeBind(this.#clearTimer, this);
constructor() {
emitExperimentalWarning('The MockTimers API');
}
Expand Down Expand Up @@ -158,7 +178,7 @@ class MockTimers {
yield* iterator;
}

#setTimeoutPromisified(ms, result, options) {
#promisifyTimer({ timerFn, clearFn, ms, result, options }) {
return new Promise((resolve, reject) => {
if (options?.signal) {
try {
Expand All @@ -173,12 +193,12 @@ class MockTimers {
}

const onabort = () => {
this.#clearTimeout(id);
clearFn(id);
return reject(abortIt(options.signal));
};

const id = this.#setTimeout(() => {
return resolve(result || id);
const id = timerFn(() => {
return resolve(result);
}, ms);

if (options?.signal) {
Expand All @@ -192,6 +212,28 @@ class MockTimers {
});
}

#setImmediatePromisified(result, options) {
return this.#promisifyTimer({
__proto__: null,
timerFn: FunctionPrototypeBind(this.#setImmediate, this),
clearFn: FunctionPrototypeBind(this.#clearImmediate, this),
ms: TIMERS_DEFAULT_INTERVAL.setImmediate,
result,
options,
});
}

#setTimeoutPromisified(ms, result, options) {
return this.#promisifyTimer({
__proto__: null,
timerFn: FunctionPrototypeBind(this.#setTimeout, this),
clearFn: FunctionPrototypeBind(this.#clearTimeout, this),
ms,
result,
options,
});
}

#toggleEnableTimers(activate) {
const options = {
__proto__: null,
Expand Down Expand Up @@ -233,6 +275,23 @@ class MockTimers {
this,
);
},
setImmediate: () => {
this.#realSetImmediate = globalThis.setImmediate;
this.#realClearImmediate = globalThis.clearImmediate;
this.#realTimersSetImmediate = nodeTimers.setImmediate;
this.#realTimersClearImmediate = nodeTimers.clearImmediate;

globalThis.setImmediate = this.#setImmediate;
globalThis.clearImmediate = this.#clearImmediate;

nodeTimers.setImmediate = this.#setImmediate;
nodeTimers.clearImmediate = this.#clearImmediate;

nodeTimersPromises.setImmediate = FunctionPrototypeBind(
this.#setImmediatePromisified,
this,
);
},
},
toReal: {
__proto__: null,
Expand All @@ -254,6 +313,15 @@ class MockTimers {

nodeTimersPromises.setInterval = this.#realPromisifiedSetInterval;
},
setImmediate: () => {
globalThis.setImmediate = this.#realSetImmediate;
globalThis.clearImmediate = this.#realClearImmediate;
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved

nodeTimers.setImmediate = this.#realTimersSetImmediate;
nodeTimers.clearImmediate = this.#realTimersClearImmediate;

nodeTimersPromises.setImmediate = this.#realPromisifiedSetImmediate;
},
},
};

Expand Down
Loading
Loading