Recall & Review
beginner
What is middleware in Express?
Middleware is a function that runs during the request-response cycle. It can modify the request or response objects, end the request, or call the next middleware.
Click to reveal answer
beginner
Why do we test middleware separately in Express apps?
Testing middleware separately helps ensure each piece works correctly on its own before combining them. It isolates logic and catches bugs early.
Click to reveal answer
beginner
Name a common tool used for testing Express middleware.
Supertest is commonly used to test Express middleware by simulating HTTP requests and checking responses.
Click to reveal answer
intermediate
How can you test if middleware calls the next() function properly?
You can mock the next() function and check if it was called during the middleware execution.
Click to reveal answer
intermediate
What is a key strategy to test middleware that modifies the request or response objects?
Create mock request and response objects, run the middleware, then check if the objects were modified as expected.
Click to reveal answer
What does Express middleware typically receive as arguments?
✗ Incorrect
Express middleware functions receive three arguments: req (request), res (response), and next (a function to call the next middleware).
Which tool is best suited for simulating HTTP requests to test middleware?
✗ Incorrect
Supertest is designed to simulate HTTP requests and test Express middleware and routes.
How do you verify that middleware correctly ends a response?
✗ Incorrect
Middleware that ends a response usually calls res.end or res.send. Testing if these were called confirms response completion.
What is a good way to test error-handling middleware?
✗ Incorrect
Error-handling middleware is triggered by passing an error to next(). Testing this ensures errors are handled properly.
Why mock req and res objects in middleware tests?
✗ Incorrect
Mocking req and res lets you test middleware logic alone, without needing a full server or network.
Explain how you would test an Express middleware that adds a property to the request object.
Think about how to simulate the request and check changes.
You got /4 concepts.
Describe strategies to test middleware that handles errors in Express.
Focus on how error middleware is triggered and verified.
You got /4 concepts.