Mistake: Not Restoring Mock
Find the Mistake Medium
Question
What is wrong with this spy usage?
Code
test('spy test', () => {
jest.spyOn(console, 'log').mockImplementation(() => {});
console.log('hello');
// test continues...
});
Hint
The spy replaces console.log permanently if not restored.
Explanation
The mistake is not restoring the spy after the test. console.log remains mocked for all subsequent tests, which can cause confusing failures. The fix: call spy.mockRestore() in an afterEach, or use jest.restoreAllMocks() in afterEach. Better yet, use jest.spyOn within a beforeEach/afterEach pair.
Related Resources
Related Lessons
- Mocking
Using mocks to isolate code.
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.