Skip to content

Commit

Permalink
test_runner: preserve original property descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
ErickWendel committed Sep 1, 2023
1 parent df16705 commit 3fda352
Showing 1 changed file with 137 additions and 35 deletions.
172 changes: 137 additions & 35 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const {
DateNow,
FunctionPrototypeApply,
FunctionPrototypeBind,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
Promise,
SymbolAsyncIterator,
SymbolDispose,
Expand Down Expand Up @@ -239,11 +241,26 @@ class MockTimers {
toFake: {
__proto__: null,
setTimeout: () => {
this.#realSetTimeout = globalThis.setTimeout;
this.#realClearTimeout = globalThis.clearTimeout;
this.#realTimersSetTimeout = nodeTimers.setTimeout;
this.#realTimersClearTimeout = nodeTimers.clearTimeout;
this.#realPromisifiedSetTimeout = nodeTimersPromises.setTimeout;
this.#realSetTimeout = ObjectGetOwnPropertyDescriptor(
globalThis,
'setTimeout',
);
this.#realClearTimeout = ObjectGetOwnPropertyDescriptor(
globalThis,
'clearTimeout',
);
this.#realTimersSetTimeout = ObjectGetOwnPropertyDescriptor(
nodeTimers,
'setTimeout',
);
this.#realTimersClearTimeout = ObjectGetOwnPropertyDescriptor(
nodeTimers,
'clearTimeout',
);
this.#realPromisifiedSetTimeout = ObjectGetOwnPropertyDescriptor(
nodeTimersPromises,
'setTimeout',
);

globalThis.setTimeout = this.#setTimeout;
globalThis.clearTimeout = this.#clearTimeout;
Expand All @@ -257,11 +274,26 @@ class MockTimers {
);
},
setInterval: () => {
this.#realSetInterval = globalThis.setInterval;
this.#realClearInterval = globalThis.clearInterval;
this.#realTimersSetInterval = nodeTimers.setInterval;
this.#realTimersClearInterval = nodeTimers.clearInterval;
this.#realPromisifiedSetInterval = nodeTimersPromises.setInterval;
this.#realSetInterval = ObjectGetOwnPropertyDescriptor(
globalThis,
'setInterval',
);
this.#realClearInterval = ObjectGetOwnPropertyDescriptor(
globalThis,
'clearInterval',
);
this.#realTimersSetInterval = ObjectGetOwnPropertyDescriptor(
nodeTimers,
'setInterval',
);
this.#realTimersClearInterval = ObjectGetOwnPropertyDescriptor(
nodeTimers,
'clearInterval',
);
this.#realPromisifiedSetInterval = ObjectGetOwnPropertyDescriptor(
nodeTimersPromises,
'setInterval',
);

globalThis.setInterval = this.#setInterval;
globalThis.clearInterval = this.#clearInterval;
Expand All @@ -275,10 +307,26 @@ class MockTimers {
);
},
setImmediate: () => {
this.#realSetImmediate = globalThis.setImmediate;
this.#realClearImmediate = globalThis.clearImmediate;
this.#realTimersSetImmediate = nodeTimers.setImmediate;
this.#realTimersClearImmediate = nodeTimers.clearImmediate;
this.#realSetImmediate = ObjectGetOwnPropertyDescriptor(
globalThis,
'setImmediate',
);
this.#realClearImmediate = ObjectGetOwnPropertyDescriptor(
globalThis,
'clearImmediate',
);
this.#realTimersSetImmediate = ObjectGetOwnPropertyDescriptor(
nodeTimers,
'setImmediate',
);
this.#realTimersClearImmediate = ObjectGetOwnPropertyDescriptor(
nodeTimers,
'clearImmediate',
);
this.#realPromisifiedSetImmediate = ObjectGetOwnPropertyDescriptor(
nodeTimersPromises,
'setImmediate',
);

globalThis.setImmediate = this.#setImmediate;
globalThis.clearImmediate = this.#clearImmediate;
Expand All @@ -295,31 +343,85 @@ class MockTimers {
toReal: {
__proto__: null,
setTimeout: () => {
globalThis.setTimeout = this.#realSetTimeout;
globalThis.clearTimeout = this.#realClearTimeout;

nodeTimers.setTimeout = this.#realTimersSetTimeout;
nodeTimers.clearTimeout = this.#realTimersClearTimeout;

nodeTimersPromises.setTimeout = this.#realPromisifiedSetTimeout;
ObjectDefineProperty(
globalThis,
'setTimeout',
this.#realSetTimeout,
);
ObjectDefineProperty(
globalThis,
'clearTimeout',
this.#realClearTimeout,
);
ObjectDefineProperty(
nodeTimers,
'setTimeout',
this.#realSetTimeout,
);
ObjectDefineProperty(
nodeTimers,
'clearTimeout',
this.#realTimersClearTimeout,
);
ObjectDefineProperty(
nodeTimersPromises,
'setTimeout',
this.#realPromisifiedSetTimeout,
);
},
setInterval: () => {
globalThis.setInterval = this.#realSetInterval;
globalThis.clearInterval = this.#realClearInterval;

nodeTimers.setInterval = this.#realTimersSetInterval;
nodeTimers.clearInterval = this.#realTimersClearInterval;

nodeTimersPromises.setInterval = this.#realPromisifiedSetInterval;
ObjectDefineProperty(
globalThis,
'setInterval',
this.#realSetInterval,
);
ObjectDefineProperty(
globalThis,
'clearInterval',
this.#realClearInterval,
);
ObjectDefineProperty(
nodeTimers,
'setInterval',
this.#realTimersSetInterval,
);
ObjectDefineProperty(
nodeTimers,
'clearInterval',
this.#realTimersClearInterval,
);
ObjectDefineProperty(
nodeTimersPromises,
'setInterval',
this.#realPromisifiedSetInterval,
);
},
setImmediate: () => {
globalThis.setImmediate = this.#realSetImmediate;
globalThis.clearImmediate = this.#realClearImmediate;

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

nodeTimersPromises.setImmediate = this.#realPromisifiedSetImmediate;
ObjectDefineProperty(
globalThis,
'setImmediate',
this.#realSetImmediate,
);
ObjectDefineProperty(
globalThis,
'clearImmediate',
this.#realClearImmediate,
);
ObjectDefineProperty(
nodeTimers,
'setImmediate',
this.#realTimersSetImmediate,
);
ObjectDefineProperty(
nodeTimers,
'clearImmediate',
this.#realTimersClearImmediate,
);
ObjectDefineProperty(
nodeTimersPromises,
'setImmediate',
this.#realPromisifiedSetImmediate,
);
},
},
};
Expand Down

0 comments on commit 3fda352

Please sign in to comment.