0
0
Node.jsframework~10 mins

Mocking modules and functions in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mocking modules and functions
Test starts
Import module
Mock function or module
Run code using mock
Check mock calls or results
Test ends
This flow shows how a test imports a module, replaces parts with mocks, runs code, and checks mock behavior.
Execution Sample
Node.js
import { jest } from '@jest/globals';
import fs from 'fs';

jest.mock('fs');

fs.readFileSync.mockReturnValue('mocked content');

const content = fs.readFileSync('file.txt', 'utf8');
This code mocks the fs module's readFileSync function to return 'mocked content' instead of reading a file.
Execution Table
StepActionEvaluationResult
1Import fs modulefs is real modulefs points to real fs module
2jest.mock('fs')Replace fs with mockfs.readFileSync is now a mock function
3fs.readFileSync.mockReturnValue('mocked content')Set mock returnreadFileSync returns 'mocked content' when called
4Call fs.readFileSync('file.txt', 'utf8')Call mock functionReturns 'mocked content'
5Check content variablecontent = 'mocked content'content holds mocked string
💡 Test ends after verifying mock returns expected value
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
fs.readFileSyncreal functionmock functionmock function with return 'mocked content'called mock functionmock function
contentundefinedundefinedundefined'mocked content''mocked content'
Key Moments - 2 Insights
Why does fs.readFileSync return 'mocked content' instead of reading a file?
Because jest.mock replaced fs.readFileSync with a mock function, and mockReturnValue set its return to 'mocked content' (see execution_table step 3 and 4).
Does jest.mock replace the whole module or just one function?
jest.mock replaces the whole module with a mock version. Individual functions like readFileSync become mock functions you can control (see execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'content' after step 4?
Aundefined
B'mocked content'
Cfile contents from disk
Dnull
💡 Hint
Check the 'content' variable in variable_tracker after step 4
At which step does fs.readFileSync become a mock function?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
See execution_table step 2 where jest.mock replaces fs with a mock
If we remove jest.mock('fs'), what would fs.readFileSync return at step 4?
A'mocked content'
Bundefined
CActual file content from disk
DAn error
💡 Hint
Without mocking, fs.readFileSync calls the real function (see execution_table step 1)
Concept Snapshot
Mocking modules and functions in Node.js tests:
- Use jest.mock('module') to replace a module with mocks.
- Mock functions can have return values set with mockReturnValue.
- Calls to mocked functions return mocked data, not real data.
- This helps isolate tests from real dependencies.
- Always check mock calls and results in tests.
Full Transcript
In Node.js testing, mocking means replacing real modules or functions with fake versions that return controlled data. The test starts by importing the module, then jest.mock replaces it with a mock. You can set what the mock returns using mockReturnValue. When the code calls the mocked function, it returns the fake data instead of doing the real work. This lets tests run without depending on real files or network. The execution table shows each step: importing, mocking, setting return, calling, and checking results. Variables like fs.readFileSync change from real to mock functions. This approach helps write reliable tests that focus on the code logic, not external factors.