0
0
Expressframework~30 mins

Integration vs unit test decision in Express - Hands-On Comparison

Choose your learning style9 modes available
Integration vs Unit Test Decision in Express
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use test() from jest and request(app).get('/books') from supertest for integration test.