In testing frameworks like Jest, a common feature is spyOn, which allows you to spy on methods of objects to see how they are called, and optionally replace them with a mock implementation.
Can you implement a simple version of spyOn that allows you to track calls to a method on an object?
Your spyOn function should:
const obj = { greet(name) { return `Hello, ${name}!`; } }; const spy = spyOn(obj, 'greet'); obj.greet('Alice'); // Call the method console.log(spy.calls); // Should output: [['Alice']] console.log(spy.results); // Should output: ['Hello, Alice!'] spy.restore(); // Restore the original method console.log(obj.greet('Bob')); // Should output: 'Hello, Bob!'
Implement the spyOn function and ensure it behaves as described.