0
0
Expressframework~20 mins

Integration vs unit test decision in Express - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to use unit tests in Express apps?

Which scenario best fits writing a unit test in an Express application?

ATesting the deployment process of the Express app on a cloud server.
BTesting the entire API endpoint including database calls and middleware together.
CTesting the integration between two microservices communicating over HTTP.
DTesting a single middleware function's logic in isolation without involving the server or database.
Attempts:
2 left
💡 Hint

Unit tests focus on small parts of code working alone.

🧠 Conceptual
intermediate
2:00remaining
When to prefer integration tests in Express apps?

Which situation is best suited for an integration test in an Express app?

AChecking if a route handler correctly processes a request and saves data to the database.
BVerifying a utility function that formats dates returns the correct string.
CTesting if a single middleware function throws an error on invalid input.
DChecking if the Express app starts without syntax errors.
Attempts:
2 left
💡 Hint

Integration tests check multiple parts working together.

component_behavior
advanced
2:00remaining
What happens when you run this Express route unit test?

Consider this unit test for an Express middleware that adds a header. What will the test output be?

Express
const req = {};
const res = { setHeader: jest.fn() };
const next = jest.fn();

function addCustomHeader(req, res, next) {
  res.setHeader('X-Custom', '123');
  next();
}

addCustomHeader(req, res, next);

console.log(res.setHeader.mock.calls.length);
A2
B0
CThrows ReferenceError because jest is not defined
D1
Attempts:
2 left
💡 Hint

Think about the environment where this code runs and what jest.fn() means.

state_output
advanced
2:00remaining
What is the final state after this integration test runs?

Given this Express route and test, what will be the value of userCount after the test?

Express
let userCount = 0;

const express = require('express');
const app = express();
app.use(express.json());

app.post('/users', (req, res) => {
  userCount += 1;
  res.status(201).send({ id: userCount });
});

// Simulate test
const request = require('supertest');

(async () => {
  await request(app).post('/users').send({ name: 'Alice' });
  await request(app).post('/users').send({ name: 'Bob' });
  console.log(userCount);
})();
A0
B2
C1
DThrows error because supertest is not installed
Attempts:
2 left
💡 Hint

Each POST request increments userCount.

🔧 Debug
expert
2:00remaining
Why does this integration test fail with timeout?

Look at this Express integration test code. Why does the test timeout instead of passing?

Express
const express = require('express');
const app = express();

app.get('/ping', (req, res) => {
  setTimeout(() => res.send('pong'), 1000);
});

const request = require('supertest');

test('GET /ping returns pong', async () => {
  const response = await request(app).get('/ping');
  expect(response.text).toBe('pong');
});
AThe test times out because Jest timeout is less than 1000ms and the response is delayed.
BThe test times out because the route handler never calls res.send.
CThe test times out because supertest does not support async/await syntax.
DThe test times out because the Express app is missing app.listen call.
Attempts:
2 left
💡 Hint

Consider how long the route delays response and Jest's default timeout.