Testing and assertion libraries like Jasmine, Mocha, Chai, Sinon etc. have a special feature called spies. Spies allow test specs to track how specific functions of interest are used: whether they are called, how many times, what they are called with, what they return, if they throw errors, etc.
For this REACTO problem, implement a spyOn
function which does the following:
- Takes a function
func
as its parameter - Returns a spy function
spy
that takes any number of arguments spy
callsfunc
with the given arguments and returns whatfunc
returnsspy
has the following methods:.getCallCount()
: returns the number of timesspy
has been called.wasCalledWith(val)
: returnstrue
ifspy
was ever called withval
, else returnsfalse
.returned(val)
: returnstrue
ifspy
ever returnedval
, else returnsfalse
Below is a specific example of how spyOn
might work in the wild. Keep in mind that not all functions take only two arguments…
function adder(n1, n2) { return n1 + n2; }
const adderSpy = spyOn( adder );
adderSpy.getCallCount(); // 0
adderSpy(2, 4); // returns 6
adderSpy.getCallCount(); // 1
adderSpy(3, 5); // returns 8
adderSpy.getCallCount(); // 2
adderSpy.wasCalledWith(2); // true
adderSpy.wasCalledWith(0); // false
adderSpy.returned(6); // true
adderSpy.returned(9); // false
function spyOn (func) {
let callCount = 0;
const calledWith = new Set();
const returnVals = new Set();
function spy (...args) {
const result = func(...args);
callCount++;
args.forEach(arg => calledWith.add(arg));
returnVals.add(result);
return result;
}
spy.getCallCount = function () {
return callCount;
};
spy.wasCalledWith = function (argument) {
return calledWith.has(argument);
};
spy.returned = function (result) {
return returnVals.has(result);
};
return spy;
}
module.exports = spyOn;