# API Integration Tests with a Real Database

> Test HTTP endpoints end to end on the server with a real PostgreSQL test database, covering auth, validation and access control. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/full-stack/api-integration-testing
Last updated: 2026-11-28

Test HTTP endpoints end to end on the server with a real PostgreSQL test database, covering auth, validation and access control.

Intermediate · about 6 hours · difficulty 3/5

Learn first: [Testing Fundamentals with Vitest](https://www.internhack.in/roadmaps/tracks/full-stack/testing-basics), [Authorization and Access Control](https://www.internhack.in/roadmaps/tracks/full-stack/authorization-rbac)

Integration tests send real HTTP requests to your API and let it talk to a real database. They catch the bugs unit tests miss: wrong queries, missing migrations, broken auth middleware.

### Setup

- Run a separate **test database**, usually PostgreSQL in Docker or a Testcontainers instance.
- Apply migrations before the test run.
- Reset data between tests by truncating tables or wrapping each test in a transaction that is rolled back.
- Use **Supertest** for Express or Fastify's inject method to call routes without opening a port. For Next.js route handlers, call the exported functions with a Request object.

### What to cover

- Happy paths for each endpoint.
- Validation errors return 400 with field messages.
- Unauthenticated requests get 401; other users' records give 403 or 404.
- Database constraints: duplicate applications return 409.

### Test data

Use small factory functions (createStudent, createOpening) instead of giant shared fixtures, so each test sets up exactly what it needs.

### Pitfalls

- Pointing tests at your development or, worse, production database.
- Tests that depend on run order or leftover data.
- Mocking the ORM in integration tests, which removes the point of them.

You are ready to move on when your API has integration tests for every route, including auth and cross-user access checks, running against a disposable PostgreSQL database.

## Key concepts

- **Test database setup**
- **Resetting state between tests**
- **Supertest or inject**
- **Testing auth and permissions**
- **Factories for test data**
- **Testcontainers**

## Mini project

Add an integration test suite for your placement API with a Dockerised test database, factories, and tests for validation, auth, cross-user access and duplicate application conflicts.

## Check yourself

Two tests pass alone but fail when run together. What are the most likely causes?

## Free resources

- [Supertest](https://github.com/ladjs/supertest) (ladjs)
- [Testcontainers for Node.js](https://node.testcontainers.org/) (Testcontainers)
- [Prisma: Integration testing](https://www.prisma.io/docs/orm/prisma-client/testing/integration-testing) (Prisma)

Previous: [Component Testing with React Testing Library](https://www.internhack.in/roadmaps/tracks/full-stack/component-testing) · Next: [End-to-End Testing with Playwright](https://www.internhack.in/roadmaps/tracks/full-stack/e2e-playwright)

## More in Testing the Whole Stack

- [Testing Fundamentals with Vitest](https://www.internhack.in/roadmaps/tracks/full-stack/testing-basics)
- [Component Testing with React Testing Library](https://www.internhack.in/roadmaps/tracks/full-stack/component-testing)
- [API Integration Tests with a Real Database](https://www.internhack.in/roadmaps/tracks/full-stack/api-integration-testing)
- [End-to-End Testing with Playwright](https://www.internhack.in/roadmaps/tracks/full-stack/e2e-playwright)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Full-Stack Developer Roadmap](https://www.internhack.in/roadmaps/tracks/full-stack)
