Spy on a Function

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write spy(fn), a small version of the mock functions test libraries give you. It returns a function that calls fn with the same arguments and the same this, and returns fn's result. The returned function has a calls property: an array holding each call's arguments as an array, in the order the calls happened, starting empty. It also has a reset() method that empties calls; recording carries on afterwards.

Your code must pass

  • Returns what fn returns
  • Records each call's arguments in order
  • Passes this through to fn
  • reset() clears the record and recording carries on