Skip to content

Integration Testing

The Integration Testing Framework is designed for robust API testing with efficient data setup.

  • “Black Box” API Testing: We test the public interface (API endpoints) by sending real HTTP requests.
  • “White Box” Data Setup: We use direct database access (via Prisma) to seed data. This avoids the slowness of utilizing the API for setup.

Tests live in tests/integration/api/.

import { describe, it, expect } from 'vitest';
import { ApiClient } from '@tests/integration/lib/client';
import { Factory } from '@tests/integration/lib/factory';
import { TestServer } from '@tests/integration/lib/server';

Use the .as() method to switch contexts fluently.

const client = new ApiClient(TestServer.getUrl());
// Authenticate as a user (creates user + logs in via session automatically)
await client.as('user', { role: 'ADMIN', name: 'Alice' });
const response = await client.get('/api/users');
expect(response.status).toBe(200);

Use Factory to create state directly in the DB.

const team = await Factory.create('team', {
name: 'Integration Team',
members: {
create: { userId: user.id, role: 'OWNER' },
},
});
Terminal window
# Run all
npm run test:integration
# Run specific
npm run test:integration tests/integration/api/teams.test.ts