Testing helps find bugs early. Unit tests check small parts alone. Integration tests check parts working together.
When you want to check if a single function or route works correctly by itself.
When you want to verify that multiple parts like routes, database, and middleware work together.
When you fix a bug in one function and want to quickly confirm it works.
When you add a new feature that involves several parts interacting.
When you want to catch errors caused by how parts connect, not just inside one part.
Syntax
Express
Unit test example:
describe('functionName', () => {
it('does something', () => {
// test code here
});
});
Integration test example:
describe('API route', () => {
it('responds with data', async () => {
// test code here
});
});
Unit tests focus on one small piece, like a single function or module.
Integration tests check how multiple parts work together, like routes and database.
Examples
This is an integration test checking the /users route returns a list.
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.