Skip to content

Commit

Permalink
test_runner: refactoring functions
Browse files Browse the repository at this point in the history
Signed-off-by: Erick Wendel <erick.workspace@gmail.com>
  • Loading branch information
ErickWendel committed Sep 1, 2023
1 parent 77caf13 commit 93a9e75
Showing 1 changed file with 159 additions and 135 deletions.
294 changes: 159 additions & 135 deletions lib/internal/test_runner/mock/mock_timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,7 @@ class MockTimers {
toFake: {
__proto__: null,
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',
);
this.#storeOriginalSetTimeout();

globalThis.setTimeout = this.#setTimeout;
globalThis.clearTimeout = this.#clearTimeout;
Expand All @@ -274,26 +255,7 @@ class MockTimers {
);
},
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',
);
this.#storeOriginalSetInterval();

globalThis.setInterval = this.#setInterval;
globalThis.clearInterval = this.#clearInterval;
Expand All @@ -307,26 +269,7 @@ class MockTimers {
);
},
setImmediate: () => {
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',
);
this.#storeOriginalSetImmediate();

globalThis.setImmediate = this.#setImmediate;
globalThis.clearImmediate = this.#clearImmediate;
Expand All @@ -343,85 +286,13 @@ class MockTimers {
toReal: {
__proto__: null,
setTimeout: () => {
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,
);
this.#restoreOriginalSetTimeout();
},
setInterval: () => {
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,
);
this.#restoreOriginalSetInterval();
},
setImmediate: () => {
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,
);
this.#restoreSetImmediate();
},
},
};
Expand All @@ -431,6 +302,159 @@ class MockTimers {
this.#isEnabled = activate;
}

#restoreSetImmediate() {
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
);
}

#restoreOriginalSetInterval() {
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
);
}

#restoreOriginalSetTimeout() {
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
);
}

#storeOriginalSetImmediate() {
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'
);
}

#storeOriginalSetInterval() {
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'
);
}

#storeOriginalSetTimeout() {
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'
);
}

tick(time = 1) {
if (!this.#isEnabled) {
throw new ERR_INVALID_STATE(
Expand Down

0 comments on commit 93a9e75

Please sign in to comment.