testApp()
Import
Section titled “Import”const { testApp } = require('kaelum/test');Prerequisite:
supertestmust be installed in your project:Terminal window npm install supertest --save-devIf
supertestis not found,kaelum/testwill throw a clear error with the install command.
Signature
Section titled “Signature”testApp(app)Parameters
Section titled “Parameters”| Name | Type | Description |
|---|---|---|
app | KaelumApp | A Kaelum (or Express) app instance. No server needs to be started. |
Returns
Section titled “Returns”A TestClient object with the following methods:
| Method | Description |
|---|---|
get(path, options?) | HTTP GET |
post(path, options?) | HTTP POST |
put(path, options?) | HTTP PUT |
patch(path, options?) | HTTP PATCH |
delete(path, options?) | HTTP DELETE |
head(path, options?) | HTTP HEAD |
All methods return a Promise that resolves to the HTTP response (supertest response object with status, body, headers, text, etc.).
Options
Section titled “Options”All methods accept an optional second argument:
| Option | Type | Description |
|---|---|---|
body | any | Request body — auto-serialised as JSON, sets Content-Type: application/json automatically |
headers | Record<string, string> | Additional request headers |
query | Record<string, any> | Query params appended to the path |
auth.bearer | string | Sets Authorization: Bearer <token> |
auth.basic | string | Sets Authorization: Basic <base64(user:pass)> |
Examples
Section titled “Examples”const { testApp } = require('kaelum/test');const app = require('./app');
const client = testApp(app);
// GETconst res = await client.get('/users');
// POST with JSON bodyconst res = await client.post('/users', { body: { name: 'Alice', email: 'alice@example.com' },});
// With query paramsconst res = await client.get('/items', { query: { page: 1, limit: 20 },});
// With Bearer authconst res = await client.get('/admin', { auth: { bearer: 'my-token' },});
// With Basic authconst res = await client.get('/protected', { auth: { basic: 'user:password' },});
// Combining optionsconst res = await client.put('/posts/1', { body: { title: 'Updated' }, headers: { 'X-Request-Id': 'abc-123' }, auth: { bearer: token },});