Skip to content

Instantly share code, notes, and snippets.

@copperwall
Created August 19, 2019 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save copperwall/31b3f2b9c9341186133721de76dc7dd2 to your computer and use it in GitHub Desktop.
Save copperwall/31b3f2b9c9341186133721de76dc7dd2 to your computer and use it in GitHub Desktop.
const test = {
spy: function(fn) {
const calls = [];
return new Proxy(stuff, {
apply(target, thisArg, args) {
const result = target.apply(thisArg, args);
calls.push([args, result]);
},
get(target, p){
if (p === 'calls') {
return calls;
}
return target[p];
}
});
}
};
function stuff(arg1, arg2) {
return `${arg1} ${arg2} cool`;
}
function doSomeStuff(fn) {
return stuff(1, 2);
}
const spyStuff = test.spy(stuff);
spyStuff("hello", "cool");
doSomeStuff(spyStuff);
const calls = spyStuff.calls;
assert(calls[0][0][0] === "hello");
assert(calls[1][1] === "hello cool cool");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment