38. implement `jest.spyOn()`

easy  - accepted / - tried

If you did unit test before, you must be familiar with Spy.

You are asked to create a spyOn(object, methodName), which works the same as jest.spyOn().

To make it simple, here are the 2 requirements of spyOn

  1. original method should be called when spied one is called
  2. spy should have a calls array, which holds all the arguments in each call.

Code to explain everything.

const obj = {   data: 1,    increment(num) {      this.data += num   }}const spy = spyOn(obj, 'increment')obj.increment(1)console.log(obj.data) // 2obj.increment(2)console.log(obj.data) // 4console.log(spy.calls)

Think aloud!

(80)