Fill: jest.spyOn
Fill in the Blank Medium
Question
Fill in the blank to spy on console.log:
Code
const spy = ___(console, 'log');
console.log('hello');
expect(spy).toHaveBeenCalled();
Hint
Jest has a spyOn function.
Explanation
The correct answer is jest.spyOn. jest.spyOn(object, methodName) wraps an existing method with a spy that records calls while still calling the original implementation. You can later assert on how the method was called.
Related Resources
Related Lessons
- Stubs and Spies
Understanding test doubles.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.