Last active
August 9, 2022 17:23
-
-
Save andresroberto/cc77650e956127b466477b76b585e7fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import assert from 'node:assert'; | |
/** | |
* Executes a provided function a specific number of times | |
* @param {Function} fn A function to execute | |
* @param {number} times How many times the function should be executed | |
*/ | |
function executeTimes(fn, times) { | |
if (times < 1 || typeof fn !== 'function') { | |
return; | |
} | |
Array(times).fill(undefined).forEach(fn); | |
} | |
// Let's track `executeTimes`! | |
const tracker = new assert.CallTracker(); | |
function sayHi() { | |
console.log('Hi'); | |
} | |
// sayHi is expected to be called 3 times | |
const trackedSayHi = tracker.calls(sayHi, 3); | |
// executeTimes should execute trackedSayHi 3 times | |
executeTimes(trackedSayHi, 3); | |
// if trackedSayHi wasn't executed 3 times this line will throw an error | |
tracker.verify(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment