fast-api-tester 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readme +166 -0
package/package.json
CHANGED
package/readme
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# ⚡ fast-api-tester
|
|
2
|
+
|
|
3
|
+
> A lightweight, zero-overhead API testing engine built for high-performance Node.js environments.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/fast-api-tester)
|
|
6
|
+
[](https://github.com/your-username/fast-api-tester/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
`fast-api-tester` provides a clean, framework-agnostic client to run fast HTTP assertions against REST APIs. Designed with connection pooling and low-latency execution in mind, it works seamlessly with **Vitest**, **Jest**, or Node's native **`node:assert`**.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## 📦 Installation
|
|
13
|
+
|
|
14
|
+
Install `fast-api-tester` using your preferred package manager:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
# npm
|
|
18
|
+
npm install fast-api-tester
|
|
19
|
+
|
|
20
|
+
# pnpm
|
|
21
|
+
pnpm add fast-api-tester
|
|
22
|
+
|
|
23
|
+
# yarn
|
|
24
|
+
yarn add fast-api-tester
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
For TypeScript projects, also install execution tools or test runners as development dependencies:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install -D vitest typescript @types/node
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 🚀 Quick Start
|
|
36
|
+
|
|
37
|
+
### Option A: Using Vitest (Recommended)
|
|
38
|
+
|
|
39
|
+
1. Ensure `"type": "module"` is in your `package.json`:
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
{
|
|
43
|
+
"type": "module",
|
|
44
|
+
"scripts": {
|
|
45
|
+
"test": "vitest run"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
2. Create a test file at `tests/api.test.ts`:
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
54
|
+
import { ApiEngine } from 'fast-api-tester';
|
|
55
|
+
|
|
56
|
+
describe('JSONPlaceholder API Test Suite', () => {
|
|
57
|
+
let api: ApiEngine;
|
|
58
|
+
|
|
59
|
+
beforeAll(() => {
|
|
60
|
+
api = new ApiEngine();
|
|
61
|
+
api.setBaseUrl('https://jsonplaceholder.typicode.com');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
afterAll(async () => {
|
|
65
|
+
// Clean up socket connection pools
|
|
66
|
+
await api.destroy();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('GET /posts/1 - should fetch post details', async () => {
|
|
70
|
+
const res = await api.get('/posts/1');
|
|
71
|
+
|
|
72
|
+
expect(res.statusCode).toBe(200);
|
|
73
|
+
expect(res.body).toHaveProperty('id', 1);
|
|
74
|
+
expect(res.latencyMs).toBeGreaterThan(0);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('POST /posts - should create a new post', async () => {
|
|
78
|
+
const payload = {
|
|
79
|
+
title: 'Fast API Testing',
|
|
80
|
+
body: 'Testing with fast-api-tester',
|
|
81
|
+
userId: 1,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const res = await api.post('/posts', payload);
|
|
85
|
+
|
|
86
|
+
expect(res.statusCode).toBe(201);
|
|
87
|
+
expect(res.body).toMatchObject(payload);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
3. Run your tests:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
npm test
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Option B: Standalone Script (Zero Runner Setup)
|
|
99
|
+
|
|
100
|
+
If you don't want to use a runner like Vitest or Jest, run a script directly using `tsx` and Node's native `assert`:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
// run-tests.ts
|
|
104
|
+
import assert from 'node:assert/strict';
|
|
105
|
+
import { ApiEngine } from 'fast-api-tester';
|
|
106
|
+
|
|
107
|
+
const api = new ApiEngine();
|
|
108
|
+
api.setBaseUrl('https://jsonplaceholder.typicode.com');
|
|
109
|
+
|
|
110
|
+
async function run() {
|
|
111
|
+
try {
|
|
112
|
+
console.log('⚡ Running API assertions...');
|
|
113
|
+
|
|
114
|
+
const res = await api.get('/posts/1');
|
|
115
|
+
assert.equal(res.statusCode, 200, 'Expected status code 200');
|
|
116
|
+
|
|
117
|
+
console.log(`✅ Success! Response status: ${res.statusCode} (${res.latencyMs}ms)`);
|
|
118
|
+
} catch (err) {
|
|
119
|
+
console.error('❌ Test failed:', err);
|
|
120
|
+
} finally {
|
|
121
|
+
await api.destroy();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
run();
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Run it instantly:
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
npx tsx run-tests.ts
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
---
|
|
135
|
+
|
|
136
|
+
## 🛠️ API Reference
|
|
137
|
+
|
|
138
|
+
### `new ApiEngine(options?)`
|
|
139
|
+
|
|
140
|
+
Creates a new instance of the HTTP engine.
|
|
141
|
+
|
|
142
|
+
- `setBaseUrl(url: string)`: Sets the default host URL for all relative request paths.
|
|
143
|
+
- `get(path, headers?)`: Executes an HTTP `GET` request.
|
|
144
|
+
- `post(path, body?, headers?)`: Executes an HTTP `POST` request.
|
|
145
|
+
- `put(path, body?, headers?)`: Executes an HTTP `PUT` request.
|
|
146
|
+
- `delete(path, headers?)`: Executes an HTTP `DELETE` request.
|
|
147
|
+
- `destroy()`: Gracefully closes active network sockets and releases resources.
|
|
148
|
+
|
|
149
|
+
### Response Object Structure
|
|
150
|
+
|
|
151
|
+
All HTTP methods return a uniform response structure:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
{
|
|
155
|
+
statusCode: number; // e.g., 200, 201, 404
|
|
156
|
+
body: any; // Parsed JSON response payload
|
|
157
|
+
headers: Record<string, string | string[]>; // Response headers
|
|
158
|
+
latencyMs: number; // Round-trip duration in milliseconds
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## 📜 License
|
|
165
|
+
|
|
166
|
+
[MIT](./LICENSE) © Kushan Amarasiri
|