|
| 1 | +--- |
| 2 | +sidebar_position: 4 |
| 3 | +title: "Functional & E2E Testing" |
| 4 | +sidebar_label: "4. Functional Testing" |
| 5 | +description: "Learn how to test your API endpoints from the outside-in to ensure the business logic works for the user." |
| 6 | +--- |
| 7 | + |
| 8 | +Functional testing (often called **End-to-End** or **Black Box** testing) doesn't care about your clean code, your design patterns, or your variable names. It only cares about one thing: **"Does the feature actually work for the user?"** |
| 9 | + |
| 10 | +In the **CodeHarborHub** backend, this usually means sending a real HTTP request to your API and checking if you get the correct HTTP response. |
| 11 | + |
| 12 | +## The "Black Box" Concept |
| 13 | + |
| 14 | +Imagine your API is a black box. You can't see inside it. |
| 15 | +1. You push a button (Send a `POST` request to `/api/register`). |
| 16 | +2. Something happens inside. |
| 17 | +3. You check the result (Did I get a `201 Created` status and a Welcome email?). |
| 18 | + |
| 19 | +## Functional vs. Unit Testing |
| 20 | + |
| 21 | +| Feature | Unit Testing | Functional Testing | |
| 22 | +| :--- | :--- | :--- | |
| 23 | +| **Viewpoint** | Developer (White Box) | User (Black Box) | |
| 24 | +| **Goal** | Correctness of logic | Correctness of feature | |
| 25 | +| **Example** | Testing the `sum()` function | Testing the `Checkout` process | |
| 26 | +| **Dependencies** | Mocked (Fake) | Real (Server + DB) | |
| 27 | + |
| 28 | +## Tools for Functional Testing |
| 29 | + |
| 30 | +To test your API endpoints without opening a browser or using Postman manually, we use **Supertest**. It allows us to "simulate" HTTP requests inside our Jest tests. |
| 31 | + |
| 32 | +### Example: Testing the Signup Endpoint |
| 33 | + |
| 34 | +```javascript |
| 35 | +import request from 'supertest'; |
| 36 | +import app from '../app'; // Your Express app |
| 37 | +import { prisma } from '../lib/prisma'; |
| 38 | + |
| 39 | +describe('POST /api/auth/signup', () => { |
| 40 | + |
| 41 | + test('should create a new user and return 201', async () => { |
| 42 | + // 1. Send the request |
| 43 | + const response = await request(app) |
| 44 | + .post('/api/auth/signup') |
| 45 | + .send({ |
| 46 | + name: 'Ajay Dhangar', |
| 47 | + email: 'test@codeharborhub.com', |
| 48 | + password: 'securePassword123' |
| 49 | + }); |
| 50 | + |
| 51 | + // 2. Assert the HTTP Status |
| 52 | + expect(response.status).toBe(201); |
| 53 | + |
| 54 | + // 3. Assert the Response Body |
| 55 | + expect(response.body).toHaveProperty('id'); |
| 56 | + expect(response.body.name).toBe('Ajay Dhangar'); |
| 57 | + |
| 58 | + // 4. Verification: Is it actually in the DB? |
| 59 | + const userInDb = await prisma.user.findUnique({ |
| 60 | + where: { email: 'test@codeharborhub.com' } |
| 61 | + }); |
| 62 | + expect(userInDb).not.toBeNull(); |
| 63 | + }); |
| 64 | + |
| 65 | + test('should return 400 if email is missing', async () => { |
| 66 | + const response = await request(app) |
| 67 | + .post('/api/auth/signup') |
| 68 | + .send({ name: 'Ajay' }); |
| 69 | + |
| 70 | + expect(response.status).toBe(400); |
| 71 | + expect(response.body.message).toMatch(/required/); |
| 72 | + }); |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +## The "Happy Path" vs. "Edge Cases" |
| 77 | + |
| 78 | +In functional testing at **CodeHarborHub**, you must test both: |
| 79 | + |
| 80 | +1. **The Happy Path:** Everything goes perfectly (User enters correct data, server is up). |
| 81 | +2. **The Sad Path:** The user makes a mistake (Invalid email, password too short). |
| 82 | +3. **The Edge Case:** What happens if a user tries to register with an email that already exists? |
| 83 | + |
| 84 | +## Summary Checklist |
| 85 | + |
| 86 | + * [x] I understand that Functional Testing is "Black Box" testing. |
| 87 | + * [x] I know that Functional Tests check the API from the user's perspective. |
| 88 | + * [x] I can use **Supertest** to simulate HTTP requests. |
| 89 | + * [x] I understand the importance of testing "Sad Paths" and "Edge Cases." |
| 90 | + |
| 91 | +:::info Best Practice |
| 92 | +Functional tests are slower than unit tests because they start the entire server and talk to the database. Run them **after** your unit tests have passed to catch "big picture" bugs before you deploy to production! |
| 93 | +::: |
0 commit comments