Skip to content

Instantly share code, notes, and snippets.

@andresroberto
Last active August 9, 2022 17:23
Show Gist options
  • Save andresroberto/cc77650e956127b466477b76b585e7fc to your computer and use it in GitHub Desktop.
Save andresroberto/cc77650e956127b466477b76b585e7fc to your computer and use it in GitHub Desktop.
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