Skip to content

Commit 4d444fa

Browse files
committed
Add basic router unit tests
1 parent 6486fe7 commit 4d444fa

File tree

5 files changed

+272
-1
lines changed

5 files changed

+272
-1
lines changed

app/package-lock.json

Lines changed: 181 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"@types/jsonwebtoken": "^9.0.9",
8989
"@types/node": "^22.13.17",
9090
"@types/pg": "^8.11.11",
91+
"@types/supertest": "^6.0.3",
9192
"@types/uuid": "^10.0.0",
9293
"@typescript-eslint/eslint-plugin": "^7.16.1",
9394
"@typescript-eslint/parser": "^7.16.1",
@@ -100,6 +101,7 @@
100101
"prettier": "^3.5.3",
101102
"prisma": "^6.5.0",
102103
"rimraf": "^6.0.1",
104+
"supertest": "^7.1.1",
103105
"ts-jest": "^29.3.1",
104106
"typescript": "^5.8.2"
105107
},

app/src/utils/cache/codes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function refreshCodeCaches(): Promise<boolean> {
2929
log.debug('Codes cache refreshed');
3030
return true;
3131
} catch (error) {
32-
log.debug('Codes cache refresh failed');
32+
log.error('Codes cache refresh failed', error);
3333
return false;
3434
}
3535
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import express from 'express';
2+
import request from 'supertest';
3+
4+
import { state } from '../../../state';
5+
import router from '../../../src/routes';
6+
7+
const app = express();
8+
app.use(router);
9+
10+
describe('GET /', () => {
11+
it('should return the root information', async () => {
12+
const response = await request(app).get('/');
13+
expect(response.status).toBe(200);
14+
expect(response.body).toEqual({
15+
app: {
16+
gitRev: expect.anything(),
17+
name: expect.anything(),
18+
nodeVersion: expect.anything(),
19+
version: expect.anything()
20+
},
21+
endpoints: ['/live', '/ready', '/v1'],
22+
versions: [1]
23+
});
24+
});
25+
});
26+
27+
describe('GET /live', () => {
28+
it('should return 200 with status "ok"', async () => {
29+
const response = await request(app).get('/live');
30+
expect(response.status).toBe(200);
31+
expect(response.body).toEqual({ status: 'ok' });
32+
});
33+
});
34+
35+
describe('GET /ready', () => {
36+
beforeEach(() => {
37+
state.ready = false;
38+
});
39+
40+
it('should return 200 with status "ready" when state.ready is true', async () => {
41+
state.ready = true;
42+
const response = await request(app).get('/ready');
43+
expect(response.status).toBe(200);
44+
expect(response.body).toEqual({ status: 'ready' });
45+
});
46+
47+
it('should return 503 with status "not ready" when state.ready is false', async () => {
48+
state.ready = false;
49+
const response = await request(app).get('/ready');
50+
expect(response.status).toBe(503);
51+
expect(response.body).toEqual(expect.objectContaining({ status: 'not ready' }));
52+
});
53+
});

0 commit comments

Comments
 (0)