---
title: "Add Integration Tests to an API"
source: https://docs.autohand.ai/tutorials/add-integration-tests
---

# Add Integration Tests to an API

Write integration tests that verify your API endpoints work correctly with real database interactions, not just mocked responses.

Intermediate 15 min

autohand "Write integration tests for the /api/users endpoints. Test create, read, update, delete operations against a test database. Use Supertest with Jest. Include setup and teardown."

## What you'll learn

-   How to set up a test database and Jest global setup/teardown for integration tests
-   How to prompt Autohand to generate Supertest-based integration tests for CRUD endpoints
-   How to add authentication tests with token generation helpers
-   How to ensure test isolation so tests pass reliably in any order

## Before you start

-   Autohand Code installed
-   Jest and Supertest installed (`npm install --save-dev jest supertest`)
-   A separate test database with a `TEST_DATABASE_URL` environment variable
-   Your Express app exported from its entry file without calling `app.listen()`

## Set up the test database

Point your test runner at a dedicated test database by adding a setup file. Ask the agent to create it.

The agent reads your database client (Knex, Prisma, Sequelize, or whatever your project uses) and generates the appropriate setup. Then wire it into your Jest config.

With the setup in place, each test file can rely on a clean, migrated database at the start of the run.

## Write the test prompt

Point the agent at your routes file and describe the operations you want covered. The more specific you are, the more complete the output.

The agent reads your route file to understand the endpoint shapes before writing a single line of tests. Here is a typical route file it might read.

Notice that `beforeEach` clears the table before every test. This is test isolation. Without it, tests can affect each other in ways that are very hard to debug.

## Run the integration tests

Run the tests with your test database URL set as an environment variable.

Or add it to a `.env.test` file and load it with `dotenv`.

If a test fails on a 400 that you are not returning yet, that is a gap in your implementation, not the test. The test found a missing validation. Ask the agent to add it.

## Add authentication tests

If your API uses JWT or session-based authentication, ask the agent to add protected route tests. Give it the auth structure so it generates working tokens.

The agent adds a helper at the top of the test file that mints a token for tests.

## Handle test isolation

Integration tests that share state produce flaky results. If you see tests that pass in isolation but fail when run together, ask the agent to audit isolation.

Common isolation problems the agent looks for include tests that insert rows without cleaning up, tests that assume a specific auto-increment ID value, and tests that run in a specific order to pass.

**Tip:** Run your tests in random order periodically to catch hidden ordering dependencies. Add `"--randomize"` to your Jest command: `jest --randomize`. Flaky tests in random order are isolation problems waiting to cause CI failures.

## What you learned

-   You set up a test database with Jest global setup and teardown
-   You generated integration tests covering CRUD operations with Supertest
-   You added authentication tests with a JWT token helper
-   You audited test isolation to prevent flaky results from shared state

Try next autohand "These tests are failing. Read the test output, identify the root cause, and fix the code. Do not modify the tests unless they have a bug."

### Related tutorials

[

#### Generate Unit Tests for Existing Code

Add comprehensive unit tests to untested code with proper assertions, edge cases, and mocking.

](/docs/tutorials/generate-unit-tests.html)[

#### Run a Code Review on Your Branch

Get a thorough code review of your changes before opening a pull request.

](/docs/tutorials/run-code-review.html)