You can use a Sinon.JS stub with the callsFake() method, passing it an existing function, to effectively "wrap" the function so that it registers each time you call and with which arguments, but in a way that it also seemingly works the same way as the original function does.

For example:

function myFunction(msg) {
  console.log(msg);
}

const myStub = sinon.stub().callsFake(myFunction);

myStub('hello'); // logs "hello" to the console
myStub('world'); // logs "world"
console.log(myStub.calledTwice); // logs "true"
console.log(myStub.args[1][0]); // logs "world" (i.e. the first arg of the second call)
Previous on JavaScript
Mastodon Mastodon