Skip to content

Commit aa36690

Browse files
Add tests for sonar coverage
1 parent 957c732 commit aa36690

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
import type { Deps } from '../deps';
3+
4+
describe('getDeps()', () => {
5+
beforeEach(() => {
6+
jest.clearAllMocks();
7+
jest.resetModules();
8+
9+
// pino
10+
jest.mock('pino', () => ({
11+
__esModule: true,
12+
default: jest.fn(() => ({
13+
info: jest.fn(),
14+
error: jest.fn(),
15+
warn: jest.fn(),
16+
debug: jest.fn(),
17+
})),
18+
}));
19+
20+
jest.mock('@aws-sdk/client-s3', () => ({
21+
S3Client: jest.fn(),
22+
}));
23+
24+
// Repo client
25+
jest.mock('../../../../../internal/datastore', () => ({
26+
LetterRepository: jest.fn(),
27+
}));
28+
29+
// Env
30+
jest.mock('../env', () => ({
31+
lambdaEnv: {
32+
LETTERS_TABLE_NAME: 'LettersTable',
33+
LETTER_TTL_HOURS: '24',
34+
SUPPLIER_ID_HEADER: 'nhsd-supplier-id',
35+
APIM_CORRELATION_HEADER: 'nhsd-correlation-id',
36+
},
37+
}));
38+
});
39+
40+
test('constructs deps and wires repository config correctly', async () => {
41+
// get current mock instances
42+
const { S3Client } = jest.requireMock('@aws-sdk/client-s3') as { S3Client: jest.Mock };
43+
const pinoMock = jest.requireMock('pino') as { default: jest.Mock };
44+
const { LetterRepository } = jest.requireMock('../../../../../internal/datastore') as { LetterRepository: jest.Mock };
45+
46+
const { getDeps } = require('../deps');
47+
const deps: Deps = getDeps();
48+
49+
expect(S3Client).toHaveBeenCalledTimes(1);
50+
expect(pinoMock.default).toHaveBeenCalledTimes(1);
51+
52+
expect(LetterRepository).toHaveBeenCalledTimes(1);
53+
const repoCtorArgs = (LetterRepository as jest.Mock).mock.calls[0];
54+
expect(repoCtorArgs[2]).toEqual({
55+
lettersTableName: 'LettersTable',
56+
ttlHours: 24
57+
});
58+
59+
expect(deps.env).toEqual({
60+
LETTERS_TABLE_NAME: 'LettersTable',
61+
LETTER_TTL_HOURS: '24',
62+
SUPPLIER_ID_HEADER: 'nhsd-supplier-id',
63+
APIM_CORRELATION_HEADER: 'nhsd-correlation-id',
64+
});
65+
});
66+
67+
test('is a singleton (second call returns the same object; constructors not re-run)', async () => {
68+
// get current mock instances
69+
const { S3Client } = jest.requireMock('@aws-sdk/client-s3') as { S3Client: jest.Mock };
70+
const pinoMock = jest.requireMock('pino') as { default: jest.Mock };
71+
const { LetterRepository } = jest.requireMock('../../../../../internal/datastore') as { LetterRepository: jest.Mock };
72+
73+
const { getDeps } = require('../deps');
74+
75+
const first = getDeps();
76+
const second = getDeps();
77+
78+
expect(first).toBe(second);
79+
expect(S3Client).toHaveBeenCalledTimes(1);
80+
expect(LetterRepository).toHaveBeenCalledTimes(1);
81+
expect(pinoMock.default).toHaveBeenCalledTimes(1);
82+
});
83+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
describe('lambdaEnv', () => {
2+
const OLD_ENV = process.env;
3+
4+
beforeEach(() => {
5+
jest.resetModules(); // Clears cached modules
6+
process.env = { ...OLD_ENV }; // Clone original env
7+
});
8+
9+
afterAll(() => {
10+
process.env = OLD_ENV; // Restore
11+
});
12+
13+
it('should load all environment variables successfully', () => {
14+
process.env.SUPPLIER_ID_HEADER = 'x-supplier-id';
15+
process.env.APIM_CORRELATION_HEADER = 'x-correlation-id';
16+
process.env.LETTERS_TABLE_NAME = 'letters-table';
17+
process.env.LETTER_TTL_HOURS = '24';
18+
19+
const { lambdaEnv } = require('../env');
20+
21+
expect(lambdaEnv).toEqual({
22+
SUPPLIER_ID_HEADER: 'x-supplier-id',
23+
APIM_CORRELATION_HEADER: 'x-correlation-id',
24+
LETTERS_TABLE_NAME: 'letters-table',
25+
LETTER_TTL_HOURS: '24'
26+
});
27+
});
28+
29+
it('should throw if a required env var is missing', () => {
30+
process.env.SUPPLIER_ID_HEADER = 'x-supplier-id';
31+
process.env.APIM_CORRELATION_HEADER = 'x-correlation-id';
32+
process.env.LETTERS_TABLE_NAME = undefined; // simulate missing var
33+
process.env.LETTER_TTL_HOURS = '24';
34+
35+
expect(() => require('../env')).toThrow(
36+
'Missing required env var: LETTERS_TABLE_NAME'
37+
);
38+
});
39+
});

lambdas/api-handler/src/handlers/__tests__/get-letter-data.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ describe('API Lambda handler', () => {
4444

4545
beforeEach(() => {
4646
jest.clearAllMocks();
47-
jest.resetModules();
4847
});
4948

5049
it('returns 303 Found with a pre signed url', async () => {

0 commit comments

Comments
 (0)