Skip to main content
QA Training
All Prompts
intermediate

REST API Tests with Supertest & Jest

Generate a maintainable API test suite using Supertest and Jest covering happy paths, validation errors, auth, RBAC, and DB isolation with Zod response shape validation.

Prompt Template

You are a senior SDET specialising in Node.js API testing.

API name: {{apiName}}
Stack: {{stack}} (Express / Fastify / Next.js API routes)
Auth method: {{authMethod}}
Endpoints to test: {{endpoints}}

Generate a complete test suite using **Supertest + Jest** with the following structure:

**Test setup (jest.setup.ts / beforeAll):**
- Start the Express/Fastify app on a random port
- Run database migrations against a test database
- Seed baseline test data
- Generate auth tokens for each role (admin, user, guest)

**Per-endpoint tests (for each entry in {{endpoints}}):**

| Scenario | Expected |
|---|---|
| Happy path with valid payload | 200/201 + correct response shape |
| Missing required field | 400 + field-level error message |
| Invalid field type/format | 400 + specific validation error |
| No auth token | 401 |
| Wrong role (RBAC) | 403 |
| Resource not found | 404 |
| Duplicate resource | 409 (where applicable) |
| Payload too large | 413 (where applicable) |

**Response shape validation:**
- Define Zod schemas for every endpoint response
- Use `schema.parse(res.body)` — test fails if shape drifts from expectation

**Database isolation:**
- Wrap each test in a transaction that rolls back after the test
- Use a test-specific DB URL (never hit staging/prod)

**Auth helpers (utils/auth.ts):**
- `getAdminToken()`, `getUserToken(role)` — generate valid JWTs / session tokens
- `createTestUser(overrides)` — factory for test user creation

**File structure:**
```
__tests__/api/
  {{resource}}.test.ts  (one file per resource)
utils/
  auth.ts
  db.ts
  factories.ts
```
Tags
supertest
jest
rest-api
node
zod
database-isolation

How to use this prompt

  1. 1Copy the prompt template using the Copy button above.
  2. 2Open your preferred AI assistant (ChatGPT, Claude, Gemini, Copilot, etc.).
  3. 3Paste the prompt and replace the {{placeholder}} variables with your project-specific values.
  4. 4Review the output - treat it as a professional first draft, not a final deliverable.
  5. 5Iterate: refine the output by asking follow-up questions or providing more context.

Frequently asked questions

Which AI models work best with this prompt?v

This prompt works with any capable language model including ChatGPT (GPT-4o), Claude (3.5+), Gemini (1.5 Pro+), and GitHub Copilot Chat. More capable models produce more complete and accurate outputs.

How do I adapt the prompt for a different framework?v

Replace the framework placeholder with your target framework and update any framework-specific syntax references in the output. The structure and logic of the generated tests will transfer across frameworks.

Can I use this prompt commercially?v

Yes. All prompts on QATraining.uk are free to use for personal and commercial QA work. Attribution is appreciated but not required.