Challenge - 5 Problems
Mocking Mastery in Express
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 database call with Jest in Express?
Consider this Express route handler that uses a mocked database call. What will be the response sent to the client?
Express
const express = require('express'); const app = express(); // Mock database module const db = { getUser: jest.fn(() => Promise.resolve({ id: 1, name: 'Alice' })) }; app.get('/user', async (req, res) => { const user = await db.getUser(); res.json(user); }); // Simulate a request to /user (async () => { const req = { method: 'GET', url: '/user' }; const res = { json: (data) => console.log(JSON.stringify(data)) }; app.handle(req, res, () => {}); })();
Attempts:
2 left
💡 Hint
Think about what the mocked function returns and how the route sends the response.
✗ Incorrect
The mocked db.getUser returns a resolved promise with the user object. The route awaits this and sends it as JSON, so the output is the user object.
📝 Syntax
intermediate2:00remaining
Which option correctly mocks a database call returning a rejected promise in Jest?
You want to mock a database call that fails with an error. Which code correctly mocks this behavior?
Express
const db = {
getUser: jest.fn()
};
// Your mocking code hereAttempts:
2 left
💡 Hint
Use the Jest method designed for rejected promises.
✗ Incorrect
mockRejectedValue correctly mocks a promise that rejects with the given error. mockReturnValue with Promise.reject returns a rejected promise but is less idiomatic. mockResolvedValue returns a resolved promise, not rejected. mockReturnValue with an error object returns the error object directly, not a promise.
🔧 Debug
advanced2:30remaining
Why does this mocked database call not update the state in Express?
Given this Express route and mock, why does the response always show the initial user name instead of the updated one?
Express
const db = {
user: { id: 1, name: 'Alice' },
getUser: jest.fn(() => Promise.resolve(db.user)),
updateUser: jest.fn((newName) => { db.user.name = newName; return Promise.resolve(db.user); })
};
app.post('/user', async (req, res) => {
await db.updateUser('Bob');
const user = await db.getUser();
res.json(user);
});Attempts:
2 left
💡 Hint
Look at how the user object is shared between mocks.
✗ Incorrect
Both mocks use the same db.user object reference. updateUser changes its name property, so getUser returns the updated object. The response will show the updated name 'Bob'.
❓ lifecycle
advanced2:00remaining
What happens if you forget to reset mocks between tests in Express with Jest?
You have multiple tests mocking the same database call. What is the likely effect of not resetting mocks between tests?
Attempts:
2 left
💡 Hint
Think about how Jest tracks mock calls and implementations.
✗ Incorrect
Without resetting mocks, call counts and mock implementations persist across tests. This can cause tests to pass or fail incorrectly due to leftover state.
🧠 Conceptual
expert1:30remaining
Which option best describes the benefit of mocking database calls in Express unit tests?
Why do developers mock database calls when testing Express route handlers?
Attempts:
2 left
💡 Hint
Think about what makes unit tests fast and stable.
✗ Incorrect
Mocking database calls isolates the route handler logic from external dependencies. This makes tests faster, more predictable, and easier to run without needing a real database.