Challenge - 5 Problems
Master of Mocking Modules
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when mocking a module function with Jest?
Consider the following Node.js module and test code using Jest. What will be the output of the test?
Node.js
// math.js
export function add(a, b) {
return a + b;
}
// math.test.js
import * as math from './math';
test('mock add function', () => {
jest.spyOn(math, 'add').mockImplementation(() => 42);
const result = math.add(1, 2);
console.log(result);
});Attempts:
2 left
💡 Hint
Think about what jest.spyOn with mockImplementation does to the original function.
✗ Incorrect
The jest.spyOn replaces the original add function with a mock that returns 42 regardless of input. So calling math.add(1, 2) returns 42.
📝 Syntax
intermediate2:00remaining
Which option correctly mocks a default export function in Jest?
Given a module with a default export function, which Jest mock syntax correctly mocks it?
Node.js
// greet.js
export default function greet(name) {
return `Hello, ${name}!`;
}
// greet.test.js
import greet from './greet';
jest.mock('./greet', () => {
// Fill in the mock implementation
});Attempts:
2 left
💡 Hint
Default exports are properties named 'default' in the mock object.
✗ Incorrect
When mocking a default export, the mock must return an object with a default property that is the mocked function.
🔧 Debug
advanced2:00remaining
Why does this Jest mock not replace the imported function?
Look at this test code. Why does the mock not replace the original function when called?
Node.js
// utils.js
export function fetchData() {
return 'real data';
}
// app.js
import { fetchData } from './utils';
export function getData() {
return fetchData();
}
// app.test.js
import { getData } from './app';
import * as utils from './utils';
jest.mock('./utils');
test('mock fetchData', () => {
utils.fetchData.mockReturnValue('mocked data');
const result = getData();
console.log(result);
});Attempts:
2 left
💡 Hint
Consider when the module is imported and when the mock is applied.
✗ Incorrect
The getData function imports fetchData before the mock is applied, so it uses the original function. Jest mocks must be set up before importing the module that uses the function.
❓ state_output
advanced2:00remaining
What is the output after resetting mocks in Jest?
Given this test code, what will be the output of the console.log statements?
Node.js
const utils = {
getNumber: () => 5
};
jest.mock('./utils', () => ({
getNumber: jest.fn(() => 10)
}));
import { getNumber } from './utils';
console.log(getNumber());
getNumber.mockReset();
console.log(getNumber());Attempts:
2 left
💡 Hint
What does mockReset do to the mock function's implementation?
✗ Incorrect
mockReset clears the mock implementation, so the function returns undefined after reset.
🧠 Conceptual
expert3:00remaining
Which statement about mocking ES modules with Jest is true?
Select the correct statement about mocking ES modules and their functions in Jest.
Attempts:
2 left
💡 Hint
Think about how jest.mock replaces modules and how named exports are mocked.
✗ Incorrect
jest.mock can replace the entire module by returning an object with named exports mocked as jest.fn(). This is the standard way to mock named exports.