QA Supertest Writer
Purpose
Write API and integration tests using Supertest from test case specifications and API contracts. Transform structured test cases (from qa-testcase-from-docs, qa-manual-test-designer) and OpenAPI contracts (from qa-api-contract-curator) into executable Supertest code with chained assertions, authentication support, and response validation.
Trigger Phrases
- "Write Supertest tests for [API/endpoint]"
- "Generate API tests from OpenAPI contract"
- "Create Supertest integration tests for Express"
- "Add API tests for [endpoint] with auth"
- "Supertest tests from test cases"
- "API contract to Supertest tests"
- "Test file upload with Supertest"
- "Supertest tests for Koa server"
Key Features
| Feature |
Description |
| Express/Koa integration |
request(app) — no server startup; tests run against app instance |
| Chained HTTP assertions |
.expect(statusCode), .expect(contentType), .expect(body) — fluent API |
| Request/response validation |
Headers, status, body shape; JSON Schema or Zod/Joi for schema validation |
| Authentication support |
Bearer tokens, cookies, API keys via .set() |
| File upload testing |
.attach() for multipart/form-data |
Workflow
- Read test cases + API contract — From qa-testcase-from-docs, qa-manual-test-designer, or qa-api-contract-curator (OpenAPI)
- Generate API test files — Produce
{endpoint}.api.test.ts with describe per endpoint, it per scenario
- Add auth setup — Bearer token, cookies, or API key fixtures in
beforeEach/beforeAll
- Validate responses — Status codes, headers, body matching; optional schema validation (Zod, Joi, JSON Schema)
Context7 MCP
Use Context7 MCP for current Supertest documentation when:
- API signatures or chaining behavior are uncertain
- New Supertest features need verification
- Express vs Koa setup differences require clarification
Key Patterns
| Pattern |
Usage |
request(app) |
Wrap Express/Koa app; no listen() needed |
.get(path) / .post(path) / .put(path) / .delete(path) |
HTTP method + path |
.set(header, value) |
Set request headers (Authorization, Content-Type, etc.) |
.send(body) |
Request body (JSON, form) |
.expect(statusCode) |
Assert response status |
.expect(contentType, /json/) |
Assert Content-Type header |
.expect(body) |
Assert response body (string, regex, function) |
| Chained assertions |
.get('/users').expect(200).expect('Content-Type', /json/) |
| Bearer token |
.set('Authorization', 'Bearer ' + token) |
| Cookies |
.set('Cookie', cookieString) or use agent for session |
| File upload |
.attach(field, filePath) |
See references/patterns.md for CRUD, auth, file upload, error responses, pagination.
Test Structure
- describe per endpoint (e.g.,
describe('GET /users'))
- it per scenario: success, validation error, unauthorized, not found, conflict
- beforeAll/beforeEach for auth tokens, DB seeding, cleanup
- afterEach/afterAll for cleanup (e.g., truncate test data)
Integration with Express/Koa
import request from 'supertest'
import { app } from '../src/app'
describe('API', () => {
it('GET /health returns 200', () =>
request(app).get('/health').expect(200))
})
No app.listen() — Supertest handles the request internally.
Schema Validation
- JSON Schema — Use
ajv or similar to validate response against schema
- Zod —
zodSchema.parse(response.body) in custom .expect() callback
- Joi —
Joi.assert(response.body, schema) in callback
File Naming
{endpoint}.api.test.ts — e.g., users.api.test.ts, auth.api.test.ts
{resource}.api.test.ts — e.g., products.api.test.ts
Scope
Can do (autonomous):
- Generate Supertest API tests from test cases and OpenAPI contracts
- Add auth setup (Bearer, cookies, API key)
- Use chained assertions,
.set(), .send(), .attach()
- Configure test server, DB seeding, cleanup
- Validate responses with status, headers, body; add schema validation (Zod/Joi)
- Call qa-api-contract-curator for contract when needed
- Use Context7 MCP for Supertest docs
Cannot do (requires confirmation):
- Change production API implementation
- Add dependencies not in package.json
- Override project test config without approval
Will not do (out of scope):
- Execute tests (user runs
npm test or vitest)
- Write E2E browser tests (use qa-playwright-ts-writer)
- Modify CI/CD pipelines
References
references/patterns.md — CRUD, auth, file upload, error responses, pagination
references/assertions.md — Status codes, headers, body matching, schema validation
references/config.md — Test server config, DB seeding, cleanup
references/best-practices.md — Test isolation, DB state, auth fixtures, response validation
Quality Checklist
Troubleshooting
| Symptom |
Likely Cause |
Fix |
request(app) returns 404 |
App not mounted or route missing |
Verify app exports; check route registration order |
| Tests pass individually, fail together |
Shared DB state or auth leakage |
Reset DB in beforeEach; use fresh tokens per test |
.expect(body) fails on dynamic fields |
Body has timestamps, IDs, etc. |
Use partial match, regex, or custom callback |
| Auth not working |
Token expired or wrong header |
Check Authorization format; ensure token in scope |
| File upload fails |
Wrong field name or path |
Match field to form field; use absolute path |
| Koa app not working |
Different API than Express |
Use request(app.callback()) for Koa |
| CORS errors in tests |
CORS middleware blocking |
Supertest bypasses network; check middleware order |
1---2name: qa-supertest-writer3description: Generate Supertest API and integration tests for TypeScript/Node.js with Express/Koa integration, chained assertions, and authentication handling.4---56# QA Supertest Writer78## Purpose910Write API and integration tests using Supertest from test case specifications and API contracts. Transform structured test cases (from qa-testcase-from-docs, qa-manual-test-designer) and OpenAPI contracts (from qa-api-contract-curator) into executable Supertest code with chained assertions, authentication support, and response validation.1112## Trigger Phrases1314- "Write Supertest tests for [API/endpoint]"15- "Generate API tests from OpenAPI contract"16- "Create Supertest integration tests for Express"17- "Add API tests for [endpoint] with auth"18- "Supertest tests from test cases"19- "API contract to Supertest tests"20- "Test file upload with Supertest"21- "Supertest tests for Koa server"2223## Key Features2425| Feature | Description |26| ------- | ----------- |27| **Express/Koa integration** | `request(app)` — no server startup; tests run against app instance |28| **Chained HTTP assertions** | `.expect(statusCode)`, `.expect(contentType)`, `.expect(body)` — fluent API |29| **Request/response validation** | Headers, status, body shape; JSON Schema or Zod/Joi for schema validation |30| **Authentication support** | Bearer tokens, cookies, API keys via `.set()` |31| **File upload testing** | `.attach()` for multipart/form-data |3233## Workflow34351. **Read test cases + API contract** — From qa-testcase-from-docs, qa-manual-test-designer, or qa-api-contract-curator (OpenAPI)362. **Generate API test files** — Produce `{endpoint}.api.test.ts` with `describe` per endpoint, `it` per scenario373. **Add auth setup** — Bearer token, cookies, or API key fixtures in `beforeEach`/`beforeAll`384. **Validate responses** — Status codes, headers, body matching; optional schema validation (Zod, Joi, JSON Schema)3940## Context7 MCP4142Use **Context7 MCP** for current Supertest documentation when:43- API signatures or chaining behavior are uncertain44- New Supertest features need verification45- Express vs Koa setup differences require clarification4647## Key Patterns4849| Pattern | Usage |50| ------- | ----- |51| `request(app)` | Wrap Express/Koa app; no `listen()` needed |52| `.get(path)` / `.post(path)` / `.put(path)` / `.delete(path)` | HTTP method + path |53| `.set(header, value)` | Set request headers (Authorization, Content-Type, etc.) |54| `.send(body)` | Request body (JSON, form) |55| `.expect(statusCode)` | Assert response status |56| `.expect(contentType, /json/)` | Assert Content-Type header |57| `.expect(body)` | Assert response body (string, regex, function) |58| Chained assertions | `.get('/users').expect(200).expect('Content-Type', /json/)` |59| Bearer token | `.set('Authorization', 'Bearer ' + token)` |60| Cookies | `.set('Cookie', cookieString)` or use agent for session |61| File upload | `.attach(field, filePath)` |6263See `references/patterns.md` for CRUD, auth, file upload, error responses, pagination.6465## Test Structure6667- **describe** per endpoint (e.g., `describe('GET /users')`)68- **it** per scenario: success, validation error, unauthorized, not found, conflict69- **beforeAll/beforeEach** for auth tokens, DB seeding, cleanup70- **afterEach/afterAll** for cleanup (e.g., truncate test data)7172## Integration with Express/Koa7374```ts75import request from 'supertest'76import { app } from '../src/app'7778describe('API', () => {79 it('GET /health returns 200', () =>80 request(app).get('/health').expect(200))81})82```8384No `app.listen()` — Supertest handles the request internally.8586## Schema Validation8788- **JSON Schema** — Use `ajv` or similar to validate response against schema89- **Zod** — `zodSchema.parse(response.body)` in custom `.expect()` callback90- **Joi** — `Joi.assert(response.body, schema)` in callback9192## File Naming9394- `{endpoint}.api.test.ts` — e.g., `users.api.test.ts`, `auth.api.test.ts`95- `{resource}.api.test.ts` — e.g., `products.api.test.ts`9697## Scope9899**Can do (autonomous):**100- Generate Supertest API tests from test cases and OpenAPI contracts101- Add auth setup (Bearer, cookies, API key)102- Use chained assertions, `.set()`, `.send()`, `.attach()`103- Configure test server, DB seeding, cleanup104- Validate responses with status, headers, body; add schema validation (Zod/Joi)105- Call qa-api-contract-curator for contract when needed106- Use Context7 MCP for Supertest docs107108**Cannot do (requires confirmation):**109- Change production API implementation110- Add dependencies not in package.json111- Override project test config without approval112113**Will not do (out of scope):**114- Execute tests (user runs `npm test` or `vitest`)115- Write E2E browser tests (use qa-playwright-ts-writer)116- Modify CI/CD pipelines117118## References119120- `references/patterns.md` — CRUD, auth, file upload, error responses, pagination121- `references/assertions.md` — Status codes, headers, body matching, schema validation122- `references/config.md` — Test server config, DB seeding, cleanup123- `references/best-practices.md` — Test isolation, DB state, auth fixtures, response validation124125## Quality Checklist126127- [ ] Tests match test case steps and expected results128- [ ] Each endpoint has success and error scenarios (validation, 401, 404)129- [ ] Auth setup is correct (Bearer, cookies) and isolated per test130- [ ] No hardcoded secrets; use env vars or test fixtures131- [ ] Response assertions are specific (status, body shape, headers)132- [ ] File naming follows `{endpoint}.api.test.ts`133- [ ] DB state is reset or isolated between tests134- [ ] Schema validation used where contract specifies response shape135136## Troubleshooting137138| Symptom | Likely Cause | Fix |139| ------- | ------------ | --- |140| `request(app)` returns 404 | App not mounted or route missing | Verify app exports; check route registration order |141| Tests pass individually, fail together | Shared DB state or auth leakage | Reset DB in `beforeEach`; use fresh tokens per test |142| `.expect(body)` fails on dynamic fields | Body has timestamps, IDs, etc. | Use partial match, regex, or custom callback |143| Auth not working | Token expired or wrong header | Check `Authorization` format; ensure token in scope |144| File upload fails | Wrong field name or path | Match `field` to form field; use absolute path |145| Koa app not working | Different API than Express | Use `request(app.callback())` for Koa |146| CORS errors in tests | CORS middleware blocking | Supertest bypasses network; check middleware order |