šŸš€ LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
šŸŽ“ COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Untitled Lesson

⚔ Total XP: 0|šŸ’» backend XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Mocks bleeding state between tests because jest.fn() call history isn't cleared

// Wrong: mock call history leaks across tests const emailSpy = jest.fn(); // ...never cleared between it() blocks // Correct: reset before every test beforeEach(() => { jest.clearAllMocks(); });

The Solution //

A jest.fn() accumulates every call it has ever received across the entire test file unless explicitly reset — so a later test's toHaveBeenCalledTimes(1) can fail because a previous test already called the same mock. Use jest.clearAllMocks() (or restoreAllMocks() for spies) in a global beforeEach so every test starts with a clean slate.

The Error //

Using jest.spyOn() to wrap a global/module method but never calling mockRestore()

// Wrong: console.log stays wrapped for every later test const logSpy = jest.spyOn(console, 'log'); // Correct: always restore after use afterEach(() => { logSpy.mockRestore(); });

The Solution //

jest.spyOn(console, 'log') replaces the real console.log with a tracked wrapper for the rest of the test run — if you never call spy.mockRestore() (or use restoreMocks: true in config), every subsequent test in the file silently runs against the spied-on version instead of the real implementation, which can hide legitimate output or cause confusing cross-test behavior.

Continue Learning