0
0
Expressframework~20 mins

Mocking database calls in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mocking Mastery in Express
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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, () => {});
})();
ATypeError: db.getUser is not a function
Bundefined
C{"id":1,"name":"Alice"}
DSyntaxError
Attempts:
2 left
💡 Hint
Think about what the mocked function returns and how the route sends the response.
📝 Syntax
intermediate
2: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 here
Adb.getUser.mockResolvedValue(new Error('DB error'));
Bdb.getUser.mockRejectedValue('DB error');
Cdb.getUser.mockReturnValue(new Error('DB error'));
Ddb.getUser.mockReturnValue(Promise.reject(new Error('DB error')));
Attempts:
2 left
💡 Hint
Use the Jest method designed for rejected promises.
🔧 Debug
advanced
2: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);
});
AThe mock functions share the same user object, so the update should reflect; no issue here.
BThe updateUser mock does not actually change the user object because of closure scope.
CThe getUser mock returns a new object copy, so changes to db.user are not seen.
DThe asynchronous calls are not awaited properly, causing stale data.
Attempts:
2 left
💡 Hint
Look at how the user object is shared between mocks.
lifecycle
advanced
2: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?
AMocks retain call history and implementations, causing tests to interfere and produce false positives or negatives.
BMocks automatically reset after each test, so no effect occurs.
CTests will fail immediately with a Jest error about mock state.
DMocks become undefined and cause runtime errors.
Attempts:
2 left
💡 Hint
Think about how Jest tracks mock calls and implementations.
🧠 Conceptual
expert
1: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?
ATo replace Express middleware with database logic for integration testing.
BTo ensure the database schema is tested thoroughly during route handler tests.
CTo increase test execution time by adding complexity with mocks.
DTo isolate route logic from database dependencies, making tests faster and more reliable by avoiding real database access.
Attempts:
2 left
💡 Hint
Think about what makes unit tests fast and stable.