test: add API endpoint tests with Node.js test runner
- Replace the placeholder test script with tsx --test - Add tests for GET /health response status and payload - Add tests for GET /ready response status and dependency status - Use Fastify injection to test endpoints without opening a network listener - Close the app after the test suite completes
This commit is contained in:
parent
0d09e37af2
commit
ca38e1a30f
2 changed files with 40 additions and 1 deletions
|
|
@ -4,7 +4,7 @@
|
|||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"test": "tsx --test src/**/*.test.ts",
|
||||
"dev": "tsx watch src/server.ts",
|
||||
"build": "tsc -p tsconfig.json",
|
||||
"start": "node dist/server.js"
|
||||
|
|
|
|||
39
src/server.test.ts
Normal file
39
src/server.test.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import assert from "node:assert";
|
||||
import { after, describe, it } from "node:test";
|
||||
|
||||
import { buildApp } from "./server.js";
|
||||
|
||||
describe("API endpoints", () => {
|
||||
const app = buildApp();
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("GET /health returns 200 and status ok", async () => {
|
||||
const response = await app.inject({
|
||||
method: "GET",
|
||||
url: "/health",
|
||||
});
|
||||
|
||||
assert.equal(response.statusCode, 200);
|
||||
assert.deepEqual(response.json(), {
|
||||
status: "ok",
|
||||
});
|
||||
});
|
||||
|
||||
it("GET /ready returns 200 and dependency status", async () => {
|
||||
const response = await app.inject({
|
||||
method: "GET",
|
||||
url: "/ready",
|
||||
});
|
||||
|
||||
assert.equal(response.statusCode, 200);
|
||||
assert.deepEqual(response.json(), {
|
||||
status: "ready",
|
||||
dependencies: {
|
||||
ollama: "not-integrated",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue