📖 Scenario: You are building a simple Express server that manages a list of books. You want to learn how to decide when to write unit tests and when to write integration tests.
🎯 Goal: Build a small Express app with a books list, then write a unit test for a helper function and an integration test for the API endpoint.
📋 What You'll Learn
Create an Express app with a GET /books endpoint
Create a helper function to filter books by minimum rating
Write a unit test for the helper function
Write an integration test for the GET /books endpoint
💡 Why This Matters
🌍 Real World
In real projects, you often write unit tests for small functions and integration tests for API endpoints to ensure your app works correctly from small parts to full features.
💼 Career
Understanding when to write unit vs integration tests is essential for backend developers to maintain reliable and maintainable codebases.
Progress0 / 4 steps
1
DATA SETUP: Create the books data array
Create a constant array called books with these exact objects: { id: 1, title: '1984', rating: 4.5 }, { id: 2, title: 'Brave New World', rating: 4.0 }, and { id: 3, title: 'Fahrenheit 451', rating: 4.2 }.
Express
Hint
Use const books = [ ... ] with the exact objects inside.
2
CONFIGURATION: Create the filterBooksByRating helper function
Create a function called filterBooksByRating that takes books and minRating as parameters and returns books with rating greater than or equal to minRating.
Express
Hint
Use books.filter inside the function to select books with rating >= minRating.
3
CORE LOGIC: Create the Express app with GET /books endpoint
Create an Express app by importing express, calling express() to get app, and add a GET endpoint /books that returns the full books array as JSON.
Express
Hint
Import express, create app, and add app.get('/books', ...) that sends books as JSON.
4
COMPLETION: Write unit and integration tests
Write a unit test for filterBooksByRating that checks it returns books with rating >= 4.2. Then write an integration test for GET /books endpoint that checks the response status is 200 and the body contains the full books array. Use jest and supertest.
Express
Hint
Use test() from jest and request(app).get('/books') from supertest for integration test.
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
Step 1: Understand test types in Express
Unit tests focus on small pieces like functions or modules alone, without dependencies.
Step 2: Identify test purpose
Integration tests check how parts work together, so they are not isolated.
Final Answer:
Unit test -> Option A
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
Step 1: Understand unit test scope
Unit tests isolate the middleware by calling it directly with mock objects, no server needed.
Step 2: Identify correct testing method
Starting the server or manual checks are integration or manual tests, not unit tests.
Final Answer:
Test the middleware by calling it with mock request and response objects -> Option B
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
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
Step 1: Analyze the test code
The test sends an HTTP GET request to '/users' on the app, expecting status 200.
Step 2: Identify test type
This checks the route handler and middleware together, so it's an integration test.
Final Answer:
Integration test; checks the route and middleware working together -> Option C
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
Step 1: Identify the problem cause
Starting the server and sending real HTTP requests makes tests slow and flaky.
Step 2: Choose a better testing approach
Using unit tests with mocks avoids server start and speeds up tests, reducing timing issues.
Final Answer:
Rewrite the test as a unit test using mocks instead of starting the server -> Option A
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
Step 1: Understand testing goals
Unit tests check small parts fast; integration tests check how parts work together.
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.
Final Answer:
Write unit tests for the login function logic and integration tests for the full login route with database -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.