Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
Why is it a strict architectural rule to use
š» Code Challenge | +75 XP
Write the Node.js/Backend code snippet to implement a Node Mocking and spies pipeline. Include the setup and basic execution steps.
You are reviewing a Node Mocking and spies pipeline and the output is incorrect. Reorder the following pipeline stages in the correct logical order to fix the bug: Input Data, Process, Output.
Task: Reorder the blocks in logical sequence to solve the problem.
A.D.A. Interface
Adaptive Didactic Assistant

Pascual Vila
Frontend Instructor // Code Syllabus
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.