What is mocking in the context of Node.js testing?
Mocking means creating fake versions of modules or functions to control their behavior during tests. It helps isolate the code being tested by replacing real dependencies with controlled stand-ins.
Click to reveal answer
beginner
How does mocking a module help in testing?
Mocking a module lets you replace its real behavior with fake responses. This avoids side effects like network calls or database access, making tests faster and more reliable.
Click to reveal answer
beginner
Which Node.js testing library is commonly used for mocking functions and modules?
Jest is a popular testing library that includes built-in support for mocking functions and modules easily with simple APIs.
Click to reveal answer
intermediate
What does jest.mock('moduleName') do?
It tells Jest to replace the real module named 'moduleName' with a mock version during tests. You can then control what the mock returns or how it behaves.
Click to reveal answer
intermediate
How can you mock a single function inside a module without mocking the entire module?
You can import the module normally and then use jest.spyOn(module, 'functionName') to replace just that function with a mock, allowing you to control or check its calls.
Click to reveal answer
What is the main purpose of mocking in Node.js tests?
ATo replace real dependencies with controlled fake versions
BTo speed up the Node.js runtime
CTo write code without errors
DTo deploy applications faster
✗ Incorrect
Mocking replaces real dependencies with fake ones to isolate the code under test and control test behavior.
Which Jest function is used to mock an entire module?
Ajest.spyOn()
Bjest.mock()
Cjest.fn()
Djest.replace()
✗ Incorrect
jest.mock('moduleName') replaces the entire module with a mock version.
How do you mock a single function inside a module without mocking the whole module?
AUse jest.mock() on the module
BUse jest.replace() on the function
CUse jest.spyOn() on the function
DUse jest.fn() globally
✗ Incorrect
jest.spyOn(module, 'functionName') mocks only the specified function.
Why is mocking useful when a module makes network requests?
ATo log network data
BTo test network speed
CTo increase network traffic
DTo avoid real network calls during tests
✗ Incorrect
Mocking avoids real network calls, making tests faster and more reliable.
What does jest.fn() do?
ACreates a new mock function
BMocks an entire module
CRuns a test suite
DImports a module
✗ Incorrect
jest.fn() creates a new mock function that can track calls and return values.
Explain how you would mock a module and a single function inside a module in Node.js tests using Jest.
Think about replacing dependencies to control test behavior.
You got /4 concepts.
Describe why mocking is important in testing Node.js applications that use external services like databases or APIs.
Consider what happens if tests depend on real external services.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of mocking modules or functions in Node.js testing?
easy
A. To replace real modules or functions with fake ones for isolated testing
B. To speed up the production code execution
C. To permanently change the behavior of modules in the app
D. To automatically generate documentation for modules
Solution
Step 1: Understand mocking concept
Mocking means replacing real code with fake versions to test parts independently.
Step 2: Identify purpose in testing
This helps avoid slow or unreliable dependencies and focuses tests on the code itself.
Final Answer:
To replace real modules or functions with fake ones for isolated testing -> Option A
Quick Check:
Mocking = isolated testing [OK]
Hint: Mocking means faking parts to test code alone [OK]
2. Which of the following is the correct syntax to mock an entire module named utils using Jest?
easy
A. jest.spyOn('utils')
B. jest.fake('utils')
C. jest.mock('utils')
D. mock('utils')
Solution
Step 1: Recall Jest mocking syntax
Jest uses jest.mock('moduleName') to mock entire modules.
Step 2: Match syntax to options
Only jest.mock('utils') uses the correct jest.mock() function with the module name.
Final Answer:
jest.mock('utils') -> Option C
Quick Check:
jest.mock() mocks modules [OK]
Hint: Use jest.mock('moduleName') to mock whole modules [OK]
Common Mistakes:
Using jest.spyOn for whole modules instead of functions
Trying mock() or jest.fake() which don't exist
Passing module name incorrectly as a variable
3. Given the code below, what will be the output of console.log(fetchData()) after mocking?
import * as api from './api';
jest.mock('./api', () => ({
fetchData: () => 'mocked data'
}));
console.log(api.fetchData());
medium
A. 'real data'
B. 'mocked data'
C. undefined
D. Error: fetchData is not a function
Solution
Step 1: Understand jest.mock replacement
The jest.mock call replaces the entire './api' module with the provided object.
Step 2: Check the mocked function return
The mocked fetchData returns the string 'mocked data'.
Final Answer:
'mocked data' -> Option B
Quick Check:
Mocked function returns mocked data [OK]
Hint: Mocked functions return what you define in jest.mock [OK]
Common Mistakes:
Expecting original function output
Assuming undefined because of mocking
Confusing import and mock timing
4. What is wrong with this Jest mocking code snippet?
import * as math from './math';
jest.spyOn(math, 'add');
math.add.mockReturnValue(10);
console.log(math.add(2, 3));
medium
A. No error, the code works and logs 10
B. mockReturnValue cannot be used on spy without mockImplementation
C. The spy is created correctly, but math.add is not mocked properly
D. jest.spyOn should be called after calling math.add
Solution
Step 1: Understand jest.spyOn usage
jest.spyOn creates a mock function that can be controlled.
Step 2: Check mockReturnValue usage
Calling mockReturnValue(10) on the spy sets the return value to 10.
Final Answer:
No error, the code works and logs 10 -> Option A
Quick Check:
spyOn + mockReturnValue works [OK]
Hint: spyOn returns a mock you can control directly [OK]
Common Mistakes:
Thinking spyOn needs mockImplementation first
Believing spyOn must be after function call
Assuming spyOn does not mock return values
5. You want to mock only the sendEmail function from the notifications module to return true during tests, but keep other functions real. Which is the best approach?
hard
A. Use jest.mock('notifications') and redefine all functions manually
B. Replace the entire module with a fake object returning true for sendEmail
C. Import sendEmail separately and overwrite it directly
D. Use jest.spyOn(notifications, 'sendEmail').mockReturnValue(true)
Solution
Step 1: Understand partial mocking need
We want to mock only one function, not the whole module.
Step 2: Use jest.spyOn for specific function
jest.spyOn allows mocking a single function while keeping others real.
Final Answer:
Use jest.spyOn(notifications, 'sendEmail').mockReturnValue(true) -> Option D
Quick Check:
spyOn mocks single function only [OK]
Hint: Use jest.spyOn to mock one function, not whole module [OK]
Common Mistakes:
Mocking entire module unnecessarily
Trying to overwrite imported functions directly
Not using spyOn for partial mocks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.