🧪Spy (Testing)
Also known as Spy, Test Spy, spyOn
A wrapper that watches a real function and notes each call to it.
Think of it like
Think of it like a helper. They watch a worker and write down each job.
Example
Lucy wraps her logger in a spy. The real log line is still written, and the test checks it was called once with paid.
Why it matters
You get the real result and a record of the call, in one test.
Definition
Example code
javascript
const spy = jest.spyOn(shop, 'log');
shop.log('paid');
expect(spy).toHaveBeenCalledTimes(1);Common mistake
Key takeaways
- 1Wraps the real thing. The real code still runs.
- 2Records calls. Count and arguments.
- 3Clean up after. Restore it when done.