test: improve health.ts and browser.ts coverage
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Failing after 17m7s
Some checks failed
Build & Deploy to Staging / Build & Deploy to Staging (push) Failing after 17m7s
- health.ts: Added tests for timeout race, version regex edge cases, non-Error catch blocks - browser.ts: Added comprehensive edge case test for buildPdfOptions with all fields - Branch coverage: health.ts improved from 50% to 83.33% - Function coverage: health.ts improved from 75% to 100% - Overall function coverage improved from 84.46% to 84.95% - Total tests: 772 → 776 (+4 new tests)
This commit is contained in:
parent
1363c61e39
commit
bbc106f518
2 changed files with 91 additions and 0 deletions
|
|
@ -84,4 +84,55 @@ describe("GET /health", () => {
|
|||
// Verify client.release(true) was called to destroy the bad connection
|
||||
expect(mockRelease).toHaveBeenCalledWith(true);
|
||||
});
|
||||
|
||||
it("returns 503 when database health check times out (timeout race wins)", async () => {
|
||||
// Make pool.connect() hang longer than HEALTH_CHECK_TIMEOUT_MS (3000ms)
|
||||
const mockClient = {
|
||||
query: vi.fn(),
|
||||
release: vi.fn(),
|
||||
};
|
||||
|
||||
vi.mocked(pool.connect).mockImplementation(() =>
|
||||
new Promise((resolve) => {
|
||||
// Resolve after 5000ms, which is longer than the 3000ms timeout
|
||||
setTimeout(() => resolve(mockClient as any), 5000);
|
||||
})
|
||||
);
|
||||
|
||||
const res = await request(app).get("/health");
|
||||
|
||||
expect(res.status).toBe(503);
|
||||
expect(res.body.status).toBe("degraded");
|
||||
expect(res.body.database.status).toBe("error");
|
||||
expect(res.body.database.message).toContain("Database health check timed out");
|
||||
});
|
||||
|
||||
it("returns PostgreSQL for version string without PostgreSQL match", async () => {
|
||||
const mockClient = {
|
||||
query: vi.fn()
|
||||
.mockResolvedValueOnce({ rows: [{ 1: 1 }] }) // SELECT 1
|
||||
.mockResolvedValueOnce({ rows: [{ version: "MySQL 8.0.33" }] }), // No PostgreSQL in version string
|
||||
release: vi.fn(),
|
||||
};
|
||||
vi.mocked(pool.connect).mockResolvedValue(mockClient as any);
|
||||
|
||||
const res = await request(app).get("/health");
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.body.status).toBe("ok");
|
||||
expect(res.body.database.status).toBe("ok");
|
||||
expect(res.body.database.version).toBe("PostgreSQL"); // fallback when no regex match
|
||||
});
|
||||
|
||||
it("returns 503 when non-Error is thrown in catch block", async () => {
|
||||
// Make pool.connect() throw a non-Error object
|
||||
vi.mocked(pool.connect).mockRejectedValue("String error message");
|
||||
|
||||
const res = await request(app).get("/health");
|
||||
|
||||
expect(res.status).toBe(503);
|
||||
expect(res.body.status).toBe("degraded");
|
||||
expect(res.body.database.status).toBe("error");
|
||||
expect(res.body.database.message).toBe("Database connection failed");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue