c/backend › command
0votes

Write integration tests for an endpoint

PromptLow risk Tested
submitted by CommandVault editors to c/backend · 1 use

Cover the happy path, auth and the edge cases that bite in production.

Prompt · 2 variables
Write integration tests for this endpoint using {{test_framework}}:

{{endpoint_code}}

Cover at minimum:
- Success with valid input (assert body and status).
- Missing/invalid auth → 401/403.
- Validation failures for each required field.
- Not found and conflict cases.
- Rate limit or duplicate submission behavior if present.

Use factories for test data, clean up after each test, and don't mock the database unless I say so.
0 commentsreport

Guide

What you get: The agent writes integration tests against the real database covering success, 401/403, each validation rule, 404, 409 and duplicate submission, with factories and per-test cleanup. Best for Backend developers adding tests to an existing endpoint..

Before you start

  • The endpoint code (paste it into endpoint_code) and a repo where the app can be started in tests
  • A real test database the tests can create and clean (we used SQLite in memory)
  • Fill test_framework (we used Vitest + Supertest)

Worked example (Nov 14, 2026, Claude Code 2.1.318 (claude-opus-5-5); Codex 0.151.0 (gpt-5.6-sol))

Input

POST /api/workshops/:id/bookings (Express 5 + node:sqlite): bearer-token auth, member-only (403 for guests), zod validation of seats (1-4) and contactPhone, 404 for missing workshops, 409 for full workshops and repeat bookings, Idempotency-Key replay.

Result

Claude Code: 42 tests in tests/bookings.test.ts plus tests/factories.ts, each test on its own openDb(':memory:'); two `it.fails` tests document a bug it found (idempotency keys aren't scoped per user or workshop). Codex: 12 tests with in-file factories and a fresh in-memory DB per test.

Both suites pass when we run them (40 + 2 expected-fail; 12), tsc clean, no vi.mock. Codex couldn't run its tests in its sandbox (Supertest 'listen EPERM') and said so. Mutation check, 8 deliberate bugs in the endpoint: Claude Code's tests caught 8/8, Codex's 7/8 (it doesn't test the seats upper bound). Only Claude Code found the real idempotency bug.

How to check it worked

  • Run the suite, then break the endpoint on purpose (remove the 403 check, raise the seats limit) and confirm a test fails
  • grep for vi.mock / jest.mock: the database shouldn't be mocked
  • Check each test cleans up its data

Common problems

  • Only the lower bound of a range gets tested
  • Tests that can't run in the agent's sandbox (socket binding) go unverified there

Try it here

Fill in {{test_framework}}, {{endpoint_code}} above first for a better answer, or run it as is and the AI will assume sensible values.

or open in ChatGPT · Claude · Perplexity

How to use this prompt

  1. Fill in the 2 blanks above. The more specific you are, the better the answer.
  2. Copy it and paste it into ChatGPT, Claude, Gemini, Cursor or Codex, or use "Try it here" for a quick test.
  3. Read the answer critically: check anything it claims about your code or data before you rely on it.
  • {{test_framework}} Test framework
  • {{endpoint_code}} Endpoint code

More from c/backend

See all of c/backend →
0 comments

Log in to join the discussion.