frontend-challenges
frontend-challenges copied to clipboard
109 - jest.spyOn - javascript
index.js
export function spyOn(obj, methodName) {
const originalMethod = obj[methodName];
const spy = {
calls: [],
results: [],
restore: function () {
obj[methodName] = originalMethod;
},
};
obj[methodName] = function (...args) {
spy.calls.push(args);
const result = originalMethod.apply(obj, args);
spy.results.push(result);
return result;
};
return spy;
}