Skip to content

Commit 5c27813

Browse files
CCM-12875: Added more tests and updated jest config
1 parent 93dba7c commit 5c27813

File tree

5 files changed

+91
-43
lines changed

5 files changed

+91
-43
lines changed
Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,24 @@
1-
import type { Config } from 'jest';
1+
import { baseJestConfig } from '../../jest.config.base';
22

3-
const config: Config = {
4-
preset: 'ts-jest',
5-
testEnvironment: 'node',
3+
const config = baseJestConfig;
64

7-
roots: ['<rootDir>/src'],
5+
config.coveragePathIgnorePatterns = ['/__tests__/'];
86

9-
testMatch: ['**/__tests__/**/*.test.ts'],
10-
11-
collectCoverageFrom: [
12-
'src/**/*.ts',
13-
'!src/**/__tests__/**',
14-
'!src/**/*.test.ts',
15-
],
16-
17-
reporters: [
18-
'default',
19-
[
20-
'jest-html-reporter',
21-
{
22-
pageTitle: 'Test Report',
23-
outputPath: './.reports/unit/test-report.html',
24-
includeFailureMsg: true,
25-
},
26-
],
27-
],
28-
29-
coverageThreshold: {
30-
global: {
31-
branches: 80,
32-
functions: 80,
33-
lines: 80,
34-
statements: 80,
35-
},
7+
config.coverageThreshold = {
8+
global: {
9+
branches: 100,
10+
functions: 100,
11+
lines: 90,
12+
statements: -10,
3613
},
14+
};
3715

38-
moduleNameMapper: {
39-
'^utils$': '<rootDir>/../../utils/utils',
40-
'^handlers$': '<rootDir>/src/handlers',
41-
'^container$': '<rootDir>/src/container',
42-
'^config$': '<rootDir>/src/config',
43-
'^authenticator$': '<rootDir>/src/authenticator',
44-
},
16+
config.moduleNameMapper = {
17+
'^utils$': '<rootDir>/../../utils/utils',
18+
'^handlers$': '<rootDir>/src/handlers',
19+
'^container$': '<rootDir>/src/container',
20+
'^config$': '<rootDir>/src/config',
21+
'^authenticator$': '<rootDir>/src/authenticator',
4522
};
4623

4724
export default config;

lambdas/pdm-mock-api/src/__tests__/config.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ParameterStoreService, loadConfig } from 'config';
1+
import { ParameterStoreService, getEnv, loadConfig } from 'config';
22

33
describe('Config', () => {
44
const originalEnv = process.env;
@@ -12,6 +12,31 @@ describe('Config', () => {
1212
process.env = originalEnv;
1313
});
1414

15+
describe('getEnv', () => {
16+
it('should throw error when required env var is not set and no default provided', () => {
17+
delete process.env.SOME_REQUIRED_VAR;
18+
19+
expect(() => getEnv('SOME_REQUIRED_VAR')).toThrow(
20+
'Environment variable SOME_REQUIRED_VAR is required but not set',
21+
);
22+
});
23+
24+
it('should return environment variable value when set', () => {
25+
process.env.TEST_VAR = 'test-value';
26+
expect(getEnv('TEST_VAR')).toBe('test-value');
27+
});
28+
29+
it('should return default value when env var not set', () => {
30+
delete process.env.TEST_VAR;
31+
expect(getEnv('TEST_VAR', 'default-value')).toBe('default-value');
32+
});
33+
34+
it('should return env var value over default when both present', () => {
35+
process.env.TEST_VAR = 'actual-value';
36+
expect(getEnv('TEST_VAR', 'default-value')).toBe('actual-value');
37+
});
38+
});
39+
1540
describe('loadConfig', () => {
1641
it('should load config with default values', () => {
1742
delete process.env.MOCK_ACCESS_TOKEN;
@@ -53,9 +78,17 @@ describe('Config', () => {
5378
process.env.USE_NON_MOCK_TOKEN = 'FALSE';
5479
config = loadConfig();
5580
expect(config.useNonMockToken).toBe(false);
81+
82+
process.env.USE_NON_MOCK_TOKEN = 'yes';
83+
config = loadConfig();
84+
expect(config.useNonMockToken).toBe(false);
85+
86+
process.env.USE_NON_MOCK_TOKEN = '';
87+
config = loadConfig();
88+
expect(config.useNonMockToken).toBe(false);
5689
});
5790

58-
it('should throw error for missing required env var without default', () => {
91+
it('should not throw when all required env vars have default values', () => {
5992
const config = loadConfig();
6093
expect(config).toBeDefined();
6194
});

lambdas/pdm-mock-api/src/__tests__/container.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SSMClient } from '@aws-sdk/client-ssm';
12
import { createContainer } from 'container';
23

34
jest.mock('utils', () => ({
@@ -10,6 +11,8 @@ jest.mock('utils', () => ({
1011
},
1112
}));
1213

14+
jest.mock('@aws-sdk/client-ssm');
15+
1316
describe('Container', () => {
1417
let container: ReturnType<typeof createContainer>;
1518

@@ -71,4 +74,39 @@ describe('Container', () => {
7174
expect(result).toBeDefined();
7275
expect(result.isValid).toBeDefined();
7376
});
77+
78+
it('should handle USE_NON_MOCK_TOKEN configuration', () => {
79+
process.env.USE_NON_MOCK_TOKEN = 'true';
80+
const containerWithSSM = createContainer();
81+
82+
expect(containerWithSSM).toBeDefined();
83+
expect(containerWithSSM.authenticator).toBeDefined();
84+
expect(typeof containerWithSSM.authenticator).toBe('function');
85+
});
86+
87+
it('should wire getAccessToken to authenticator when using SSM token', async () => {
88+
const mockSend = jest.fn().mockResolvedValue({
89+
Parameter: { Value: 'ssm-stored-token' },
90+
});
91+
92+
process.env.USE_NON_MOCK_TOKEN = 'true';
93+
process.env.ACCESS_TOKEN_SSM_PATH = '/test/token/path';
94+
process.env.MOCK_ACCESS_TOKEN = 'unused-mock-token';
95+
96+
(SSMClient as jest.MockedClass<typeof SSMClient>).mockImplementation(
97+
() =>
98+
({
99+
send: mockSend,
100+
}) as any,
101+
);
102+
103+
const testContainer = createContainer();
104+
105+
const result = await testContainer.authenticator({
106+
headers: { Authorization: 'Bearer ssm-stored-token' },
107+
});
108+
109+
expect(result.isValid).toBe(true);
110+
expect(mockSend).toHaveBeenCalled();
111+
});
74112
});

lambdas/pdm-mock-api/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface Config {
77
logLevel: string;
88
}
99

10-
const getEnv = (key: string, defaultValue?: string): string => {
10+
export const getEnv = (key: string, defaultValue?: string): string => {
1111
// eslint-disable-next-line security/detect-object-injection
1212
const value = process.env[key];
1313
if (value === undefined && defaultValue === undefined) {

scripts/config/sonar-scanner.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ sonar.cpd.exclusions=**.test.*
1111
sonar.coverage.exclusions=tests/**, src/**/tests/**, src/**/__tests__/**, **/*.dev.*, lambdas/**/src/__tests__/**, **/jest.config.ts, **/jest.config.cjs, scripts/**/*.*, docs/**/*.*, utils/utils/src/__tests__/**, src/asyncapigenerator/example_usage.py, src/asyncapigenerator/test_generator.py, src/eventcatalogasyncapiimporter/examples.py
1212

1313
sonar.python.coverage.reportPaths=src/asyncapigenerator/coverage.xml,src/cloudeventjekylldocs/coverage.xml,src/eventcatalogasyncapiimporter/coverage.xml
14-
sonar.javascript.lcov.reportPaths=lcov.info,src/cloudevents/coverage/lcov.info,coverage/lcov.info
14+
sonar.javascript.lcov.reportPaths=lcov.info,src/cloudevents/coverage/lcov.info
1515
sonar.typescript.lcov.reportPaths=lcov.info,src/cloudevents/coverage/lcov.info

0 commit comments

Comments
 (0)