Closed kvedantmahajan closed 5 years ago
Hi @kushalmahajan,
It's exactly the same thing
const spy = jest.spyOn(utils, 'getWinner').mockImplementation((p1, p2) => p2)
console.log(spy === utils.getWinner) // true
So feel free to do whichever you want.
Umm...I was thinking along the lines of mock which I guess uses different reference/memory address for a function. And is a better choice than spy to NOT modify the original function. Hope, I'm thinking correctly!
Hmm... I'm not sure I understand what you mean. The example code you have above is basically identical to the solution. What do you think the difference is?
I will just post my solution
Now in your solution you have used reference to original
utils.getWinner
while callingmockImplementation
. But I just read from docs and feel that using a reference to spy function i.e.jest.spyOn(object, 'methodName')
would be a right handle to call further methods on it from jest.So, as you can see I called
mockImplementation
on same and then usedexpect(spy)
instead ofexpect(utils.getWinner)
.I just want to know the difference it would create with your approach by referencing original function instead of
spied
one. Same uses continues in my code snippet i.e.spy.mock.calls
andspy.mockRestore
as the tests are passing in either case