Claude Skills Guide

Claude Code Supertest API Testing Workflow

Building robust API testing workflows is essential for maintaining reliable backend services. When you combine Claude Code with Supertest, you get a powerful duo that can automate test creation, execution, and maintenance. This guide shows you how to build an effective API testing workflow that catches bugs early and keeps your services stable.

Why Supertest for API Testing

Supertest is a Node.js HTTP assertion library that provides a high-level abstraction for testing HTTP APIs. It works smoothly with Express, Fastify, and other Node.js frameworks. The library lets you make requests to your application without actually launching a server, making tests fast and reliable.

When paired with Claude Code, you can use AI assistance to generate comprehensive test suites, identify edge cases, and maintain test coverage as your API evolves. The tdd skill works particularly well for driving test-first development with Supertest.

Setting Up Your Testing Environment

First, ensure you have the necessary dependencies installed:

npm install --save-dev supertest jest @types/jest

Create a test setup file that initializes your Express app for testing:

// tests/setup.js
const request = require('supertest');
const app = require('../src/app');

module.exports = request(app);

Building Your First Supertest Request

With Supertest, you can chain assertions to verify response status, headers, and body content. Here’s a practical example testing a REST endpoint:

// tests/api/users.test.js
const request = require('../setup');

describe('Users API', () => {
  describe('GET /api/users', () => {
    it('should return a list of users', async () => {
      const response = await request
        .get('/api/users')
        .expect(200)
        .expect('Content-Type', /json/);

      expect(Array.isArray(response.body)).toBe(true);
      expect(response.body.length).toBeGreaterThan(0);
    });

    it('should support pagination parameters', async () => {
      const response = await request
        .get('/api/users?page=1&limit=10')
        .expect(200);

      expect(response.body).toHaveProperty('data');
      expect(response.body).toHaveProperty('pagination');
    });
  });
});

Using Claude Code to Generate Tests

Claude Code can accelerate test creation by analyzing your route handlers and generating comprehensive test cases. Use the claude-code-jest-unit-testing-workflow skill for best results when setting up your testing prompts.

When working with Claude Code, provide clear context about your API structure:

Generate Supertest tests for my Express API routes in src/routes/.
Focus on:
- Authentication endpoints (/api/auth/login, /api/auth/register)
- CRUD operations for /api/users, /api/products
- Error handling for 400, 401, 403, 404, 500 status codes
- Request validation for required fields

The AI will generate tests covering happy paths, edge cases, and error scenarios. Review the generated tests and add custom assertions specific to your business logic.

Testing Authentication and Protected Routes

API testing requires verifying that authentication mechanisms work correctly. Supertest makes it straightforward to test protected endpoints:

// tests/api/auth.test.js
const request = require('../setup');

describe('Authentication', () => {
  let authToken;

  beforeAll(async () => {
    const loginResponse = await request
      .post('/api/auth/login')
      .send({ email: 'test@example.com', password: 'password123' })
      .expect(200);

    authToken = loginResponse.body.token;
  });

  it('should reject requests without token', async () => {
    await request
      .get('/api/users')
      .expect(401);
  });

  it('should allow access with valid token', async () => {
    const response = await request
      .get('/api/users')
      .set('Authorization', `Bearer ${authToken}`)
      .expect(200);

    expect(response.body).toBeDefined();
  });
});

Automating Test Execution with Claude Code

Integrate your Supertest suite into CI/CD pipelines using GitHub Actions. Create a workflow that runs tests on every push:

# .github/workflows/test.yml
name: API Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm ci
      - run: npm test

The supermemory skill helps track test results across CI runs, making it easier to identify flaky tests and regression patterns over time.

Advanced Testing Patterns

For complex APIs, consider these advanced Supertest patterns:

Testing file uploads:

it('should handle file uploads', async () => {
  const response = await request
    .post('/api/uploads')
    .attach('file', './tests/fixtures/test-image.png')
    .expect(201);

  expect(response.body).toHaveProperty('url');
});

Testing WebSocket endpoints:

it('should handle real-time communication', async () => {
  const ws = new WebSocket('ws://localhost:3000');

  ws.on('open', () => {
    ws.send(JSON.stringify({ type: 'ping' }));
  });

  ws.on('message', (data) => {
    const message = JSON.parse(data);
    expect(message.type).toBe('pong');
  });
});

Best Practices for API Test Maintenance

Keeping tests maintainable requires consistent organization. Group tests by resource and feature rather than by HTTP method. Use descriptive test names that explain what behavior is being verified.

When your API changes, update tests first—this is where the tdd skill shines. Write the failing test, implement the feature, then verify the test passes. This approach ensures your test suite stays current with your API.

For documentation, use the pdf skill to generate test reports that can be shared with stakeholders. Test coverage reports help teams understand which endpoints need additional test cases.

Conclusion

Combining Claude Code with Supertest creates a powerful API testing workflow. Use Supertest’s expressive API to write clear, maintainable tests. Leverage Claude Code for test generation, maintenance suggestions, and CI/CD integration. The result is a reliable testing strategy that catches bugs early and gives confidence in your API’s correctness.

Built by theluckystrike — More at zovo.one