Bird
Raised Fist0
Expressframework~20 mins

Integration vs unit test decision in Express - Practice Questions

Choose your learning style10 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.

Practice

(1/5)
1. Which type of test in Express focuses on checking a single function or module in isolation?
easy
A. Unit test
B. Integration test
C. End-to-end test
D. Performance test

Solution

  1. Step 1: Understand test types in Express

    Unit tests focus on small pieces like functions or modules alone, without dependencies.
  2. Step 2: Identify test purpose

    Integration tests check how parts work together, so they are not isolated.
  3. Final Answer:

    Unit test -> Option A
  4. Quick Check:

    Single module check = Unit test [OK]
Hint: Unit tests isolate one part; integration tests combine parts [OK]
Common Mistakes:
  • Confusing integration tests with unit tests
  • Thinking end-to-end tests are unit tests
  • Assuming performance tests check single functions
2. Which of the following is the correct way to write a simple unit test for an Express middleware function?
easy
A. Use a browser to manually check middleware behavior
B. Test the middleware by calling it with mock request and response objects
C. Start the Express server and send HTTP requests to test the middleware
D. Run the entire app and check logs for middleware output

Solution

  1. Step 1: Understand unit test scope

    Unit tests isolate the middleware by calling it directly with mock objects, no server needed.
  2. Step 2: Identify correct testing method

    Starting the server or manual checks are integration or manual tests, not unit tests.
  3. Final Answer:

    Test the middleware by calling it with mock request and response objects -> Option B
  4. Quick Check:

    Mock objects for unit test [OK]
Hint: Unit test = call function with mocks, no server start [OK]
Common Mistakes:
  • Starting server for unit tests
  • Using manual browser checks for unit tests
  • Confusing integration test methods with unit tests
3. Consider this Express test snippet:
request(app)
  .get('/users')
  .expect(200)
  .end((err, res) => {
    if (err) throw err;
    console.log(res.body.length);
  });
What type of test is this and what does it check?
medium
A. Unit test; mocks the database call
B. Unit test; checks a single function's return value
C. Integration test; checks the route and middleware working together
D. Performance test; measures response time

Solution

  1. Step 1: Analyze the test code

    The test sends an HTTP GET request to '/users' on the app, expecting status 200.
  2. Step 2: Identify test type

    This checks the route handler and middleware together, so it's an integration test.
  3. Final Answer:

    Integration test; checks the route and middleware working together -> Option C
  4. Quick Check:

    HTTP request test = Integration test [OK]
Hint: HTTP request tests check multiple parts together [OK]
Common Mistakes:
  • Calling this a unit test
  • Assuming it mocks database calls
  • Thinking it measures performance
4. You wrote a test that starts the Express server and sends HTTP requests, but it runs very slowly and sometimes fails due to timing issues. What is the best way to fix this problem?
medium
A. Rewrite the test as a unit test using mocks instead of starting the server
B. Add more delays in the test to wait for responses
C. Increase server timeout settings in Express
D. Run the test on a faster machine

Solution

  1. Step 1: Identify the problem cause

    Starting the server and sending real HTTP requests makes tests slow and flaky.
  2. Step 2: Choose a better testing approach

    Using unit tests with mocks avoids server start and speeds up tests, reducing timing issues.
  3. Final Answer:

    Rewrite the test as a unit test using mocks instead of starting the server -> Option A
  4. Quick Check:

    Mocks speed tests, avoid server start [OK]
Hint: Mocks speed tests; avoid real server start [OK]
Common Mistakes:
  • Adding delays instead of fixing test design
  • Changing server settings unnecessarily
  • Blaming hardware instead of test approach
5. You want to test an Express app's user login flow. Which approach best balances speed and coverage by combining unit and integration tests?
hard
A. Only write integration tests for the login route to cover everything at once
B. Test manually in the browser without automated tests
C. Write unit tests for the entire Express app without any integration tests
D. Write unit tests for the login function logic and integration tests for the full login route with database

Solution

  1. Step 1: Understand testing goals

    Unit tests check small parts fast; integration tests check how parts work together.
  2. Step 2: Combine tests for best results

    Writing unit tests for login logic and integration tests for the route with database balances speed and coverage.
  3. Final Answer:

    Write unit tests for the login function logic and integration tests for the full login route with database -> Option D
  4. Quick Check:

    Combine unit + integration tests for best coverage [OK]
Hint: Use unit tests for logic, integration for routes [OK]
Common Mistakes:
  • Relying only on integration tests
  • Skipping integration tests entirely
  • Ignoring automated tests in favor of manual
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard