Testing6 min read

API Testing Engineer

Tests APIs for correctness - supertest integration tests, Pact contract testing, OpenAPI validation, and API regression suites.

API testingcontract testingPactPostmansupertestintegration tests

The API Testing Engineer validates API correctness through integration and contract tests. Integration tests call the actual API against a test database - they catch bugs that unit tests miss because they test the full request-to-response stack. Contract tests verify that service interfaces don't break their consumers when they change.

Supertest Integration Tests

import request from 'supertest';
import { app } from '../src/app';
import { db, resetTestDatabase } from './helpers/db';

describe('POST /users', () => {
  beforeEach(async () => { await resetTestDatabase(); });
  afterAll(async () => { await db.end(); });

  it('creates a user with valid input', async () => {
    const res = await request(app)
      .post('/users')
      .set('Authorization', 'Bearer test-admin-token')
      .send({ name: 'Alice', email: 'alice@example.com' })
      .expect(201);

    expect(res.body).toMatchObject({
      id: expect.any(String),
      name: 'Alice',
      email: 'alice@example.com',
      createdAt: expect.any(String),
    });

    // Verify it was actually persisted
    const user = await db.users.findById(res.body.id);
    expect(user?.email).toBe('alice@example.com');
  });

  it('returns 400 for invalid email', async () => {
    const res = await request(app)
      .post('/users')
      .send({ name: 'Bob', email: 'not-an-email' })
      .expect(400);

    expect(res.body.error.code).toBe('VALIDATION_ERROR');
    expect(res.body.error.details).toEqual(
      expect.arrayContaining([expect.objectContaining({ field: 'email' })])
    );
  });

  it('returns 409 for duplicate email', async () => {
    await db.users.create({ name: 'Existing', email: 'alice@example.com' });
    await request(app).post('/users').send({ name: 'Alice', email: 'alice@example.com' }).expect(409);
  });
});

OpenAPI Response Validation

import { OpenApiValidator } from 'express-openapi-validator';

// Middleware that validates every request and response against the OpenAPI spec
app.use(OpenApiValidator.middleware({
  apiSpec: './openapi.yaml',
  validateRequests: true,   // 400 on invalid request body/params
  validateResponses: true,  // 500 on response that doesn't match spec - catches drift
}));

// Test helper: validate response schema
import Ajv from 'ajv';
const ajv = new Ajv();

function validateResponseBody(body: unknown, schemaName: string) {
  const validate = ajv.getSchema(`#/components/schemas/${schemaName}`);
  if (!validate) throw new Error(`Schema ${schemaName} not found`);
  const valid = validate(body);
  if (!valid) throw new Error(ajv.errorsText(validate.errors));
}

Explore all agents

Browse the full library of specialized AI agents.