Skip to content

Commit 28e7a67

Browse files
authored
test(logger): simplify unit tests structure (#2942)
1 parent bba649f commit 28e7a67

17 files changed

+2404
-5112
lines changed

packages/logger/jest.config.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ module.exports = {
2626
},
2727
},
2828
coverageReporters: ['json-summary', 'text', 'lcov'],
29-
setupFiles: ['<rootDir>/tests/helpers/populateEnvironmentVariables.ts'],
29+
setupFiles: ['<rootDir>/tests/helpers/setupEnv.ts'],
3030
};

packages/logger/src/constants.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,17 @@ const LogLevel = {
2424
CRITICAL: 'CRITICAL',
2525
} as const;
2626

27-
export { LogJsonIndent, LogLevel };
27+
/**
28+
* Numeric values for each log level.
29+
*/
30+
const LogLevelThreshold = {
31+
TRACE: 6,
32+
DEBUG: 8,
33+
INFO: 12,
34+
WARN: 16,
35+
ERROR: 20,
36+
CRITICAL: 24,
37+
SILENT: 28,
38+
} as const;
39+
40+
export { LogJsonIndent, LogLevel, LogLevelThreshold };

packages/logger/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
export { Logger } from './Logger.js';
22
export { LogFormatter } from './formatter/LogFormatter.js';
33
export { LogItem } from './formatter/LogItem.js';
4-
export { LogLevel } from './constants.js';
4+
export { LogLevel, LogLevelThreshold } from './constants.js';

packages/logger/tests/helpers/populateEnvironmentVariables.ts

-15
This file was deleted.
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Reserved variables
2+
process.env._X_AMZN_TRACE_ID =
3+
'Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1';
4+
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
5+
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
6+
process.env.AWS_LAMBDA_FUNCTION_VERSION = '$LATEST';
7+
if (
8+
process.env.AWS_REGION === undefined &&
9+
process.env.CDK_DEFAULT_REGION === undefined
10+
) {
11+
process.env.AWS_REGION = 'eu-west-1';
12+
}
13+
14+
// Powertools for AWS Lambda (TypeScript) variables
15+
process.env.POWERTOOLS_LOG_LEVEL = 'DEBUG';
16+
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world';
17+
18+
jest.spyOn(console, 'error').mockImplementation();
19+
jest.spyOn(console, 'warn').mockImplementation();
20+
jest.spyOn(console, 'info').mockImplementation();
21+
jest.spyOn(console, 'debug').mockImplementation();
22+
jest.spyOn(console, 'log').mockImplementation();
23+
24+
jest.mock('node:console', () => ({
25+
...jest.requireActual('node:console'),
26+
Console: jest.fn().mockImplementation(() => ({
27+
log: jest.fn(),
28+
debug: jest.fn(),
29+
info: jest.fn(),
30+
warn: jest.fn(),
31+
error: jest.fn(),
32+
})),
33+
}));
34+
35+
declare global {
36+
namespace NodeJS {
37+
interface ProcessEnv {
38+
_X_AMZN_TRACE_ID: string | undefined;
39+
AWS_LAMBDA_FUNCTION_NAME: string | undefined;
40+
AWS_LAMBDA_FUNCTION_MEMORY_SIZE: string | undefined;
41+
AWS_LAMBDA_FUNCTION_VERSION: string | undefined;
42+
AWS_REGION: string | undefined;
43+
CDK_DEFAULT_REGION: string | undefined;
44+
POWERTOOLS_LOG_LEVEL: string | undefined;
45+
POWERTOOLS_SERVICE_NAME: string | undefined;
46+
POWERTOOLS_DEV: string | undefined;
47+
POWERTOOLS_LOGGER_LOG_EVENT: string | undefined;
48+
}
49+
}
50+
}
51+
52+
export type {};

packages/logger/tests/unit/EnvironmentVariablesService.test.ts

-221
This file was deleted.

0 commit comments

Comments
 (0)