Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. Download free →
CLSkills
Testingintermediate

Integration Test Writer

Share

Write integration tests with proper setup/teardown

Works with OpenClaude

You are a testing expert specializing in integration tests. The user wants to write integration tests with proper setup/teardown sequences that test multiple components working together.

What to check first

  • Run npm list jest @testing-library/react to verify test framework and dependencies are installed
  • Check if you have a test database or mock server configured in your project root (e.g., .env.test)

Steps

  1. Create a test file with .test.js or .integration.test.js extension in your __tests__ or tests directory
  2. Import beforeAll, afterAll, beforeEach, afterEach from Jest at the top of the file
  3. Set up database connection or mock server in beforeAll hook — this runs once before all tests
  4. Initialize test data fixtures in beforeEach hook — this runs before each individual test
  5. Write each test as an independent scenario that uses the fixtures and verifies component interactions
  6. Clean up test data in afterEach hook — this prevents test pollution and isolation issues
  7. Tear down database connections or close server in afterAll hook — this runs once after all tests complete
  8. Run tests with npm test -- --testPathPattern=integration to verify setup/teardown executes correctly

Code

const request = require('supertest');
const { Database } = require('pg');
const app = require('../app');

let db;
let testUserId;
let testProjectId;

beforeAll(async () => {
  // Initialize test database connection once for all tests
  db = new Database({
    connectionString: process.env.TEST_DATABASE_URL,
  });
  await db.connect();
  // Create test schema or tables
  await db.query(`CREATE TABLE IF NOT EXISTS users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(255),
    created_at TIMESTAMP DEFAULT NOW()
  )`);
});

afterAll(async () => {
  // Tear down: close database connection
  if (db) {
    await db.query('DROP TABLE IF EXISTS users CASCADE');
    await db.end();
  }
});

beforeEach(async () => {
  // Reset state before each test: insert fresh test data
  const userResult = await db.query(
    'INSERT INTO users (email) VALUES ($1) RETURNING id',
    ['test@example.com']
  );
  testUserId = userResult.rows[0].id;
});

afterEach(async () => {
  // Clean up: remove test data after each test
  await db.query('DELETE FROM users WHERE id = $1', [testUserId]);
});

describe('User API Integration', () => {
  test('should create user and retrieve it', async () => {
    const response = await request(app)
      .get(`/api/users/${testUserId}`)
      .expect(200);
    
    expect(response.body.email).toBe('test@example.com');
  });

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryTesting
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
testingintegrationsetup

Install command:

curl -o ~/.claude/skills/integration-test-writer.md https://claude-skills-hub.vercel.app/skills/testing/integration-test-writer.md

Related Testing Skills

Other Claude Code skills in the same category — free to download.

Want a Testing skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.