From 39438164c8266b2c3a5ffce5f82214c1899234d1 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 24 Sep 2025 19:03:21 +0200 Subject: [PATCH] style(logger): apply stricter linting --- packages/logger/src/formatter/LogFormatter.ts | 2 +- packages/logger/src/middleware/middy.ts | 6 +-- .../e2e/advancedUses.test.FunctionCode.ts | 6 ++- .../basicFeatures.middy.test.FunctionCode.ts | 4 +- .../tests/e2e/basicFeatures.middy.test.ts | 20 ++++----- .../childLogger.manual.test.FunctionCode.ts | 4 +- .../tests/e2e/childLogger.manual.test.ts | 8 ++-- .../e2e/logEventEnvVarSetting.middy.test.ts | 2 +- .../sampleRate.decorator.test.FunctionCode.ts | 2 + .../tests/e2e/sampleRate.decorator.test.ts | 4 +- packages/logger/tests/helpers/resources.ts | 2 +- .../tests/unit/injectLambdaContext.test.ts | 23 ++++++---- packages/logger/tests/unit/logBuffer.test.ts | 7 ++- packages/logger/tests/unit/logEvent.test.ts | 2 + .../tests/unit/setPowertoolsLogData.test.ts | 44 ------------------- .../logger/tests/unit/workingWithkeys.test.ts | 4 +- 16 files changed, 57 insertions(+), 83 deletions(-) delete mode 100644 packages/logger/tests/unit/setPowertoolsLogData.test.ts diff --git a/packages/logger/src/formatter/LogFormatter.ts b/packages/logger/src/formatter/LogFormatter.ts index 55bfe7088c..be14cc537d 100644 --- a/packages/logger/src/formatter/LogFormatter.ts +++ b/packages/logger/src/formatter/LogFormatter.ts @@ -139,7 +139,7 @@ abstract class LogFormatter { /** * If a specific timezone is configured and it's not the default `UTC`, * format the timestamp with the appropriate timezone offset. - **/ + */ const configuredTimezone = getStringFromEnv({ key: 'TZ', defaultValue: '', diff --git a/packages/logger/src/middleware/middy.ts b/packages/logger/src/middleware/middy.ts index 3bd10004ac..e674adab64 100644 --- a/packages/logger/src/middleware/middy.ts +++ b/packages/logger/src/middleware/middy.ts @@ -81,7 +81,7 @@ const injectLambdaContext = ( }; }; - const before = async (request: MiddyLikeRequest): Promise => { + const before = (request: MiddyLikeRequest) => { for (const logger of loggers) { if (isResetStateEnabled) { setCleanupFunction(request); @@ -102,7 +102,7 @@ const injectLambdaContext = ( } }; - const after = async (): Promise => { + const after = () => { for (const logger of loggers) { logger.clearBuffer(); @@ -112,7 +112,7 @@ const injectLambdaContext = ( } }; - const onError = async ({ error }: { error: unknown }): Promise => { + const onError = ({ error }: { error: unknown }) => { for (const logger of loggers) { if (options?.flushBufferOnUncaughtError) { logger.flushBuffer(); diff --git a/packages/logger/tests/e2e/advancedUses.test.FunctionCode.ts b/packages/logger/tests/e2e/advancedUses.test.FunctionCode.ts index 1e601c6097..93f7f19c24 100644 --- a/packages/logger/tests/e2e/advancedUses.test.FunctionCode.ts +++ b/packages/logger/tests/e2e/advancedUses.test.FunctionCode.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import { Logger } from '@aws-lambda-powertools/logger'; import { correlationPaths, @@ -18,7 +19,7 @@ const logger = new Logger({ logger.debug('a never buffered debug log'); -export const handlerManual = async (event: unknown) => { +export const handlerManual = (event: unknown) => { logger.addContext({} as Context); // we want only the cold start value logger.setCorrelationId(event, correlationPaths.EVENT_BRIDGE); @@ -42,7 +43,7 @@ export const handlerMiddy = middy() flushBufferOnUncaughtError: true, }) ) - .handler(async () => { + .handler(() => { logger.debug('a buffered debug log'); logger.info('an info log'); throw new Error('ops'); @@ -56,6 +57,7 @@ class Lambda { public async handler(_event: unknown, _context: Context) { logger.debug('a buffered debug log'); logger.info('an info log'); + await setTimeout(1); // simulate async work throw new Error('ops'); } } diff --git a/packages/logger/tests/e2e/basicFeatures.middy.test.FunctionCode.ts b/packages/logger/tests/e2e/basicFeatures.middy.test.FunctionCode.ts index 258c89299b..2765446681 100644 --- a/packages/logger/tests/e2e/basicFeatures.middy.test.FunctionCode.ts +++ b/packages/logger/tests/e2e/basicFeatures.middy.test.FunctionCode.ts @@ -26,10 +26,10 @@ const logger = new Logger({ }, }); -const testFunction = async ( +const testFunction = ( _event: TestEvent, context: Context -): TestOutput => { +): Awaited => { // Test feature 1: Context data injection (all logs should have the same context data) // Test feature 2: Event log (this log should have the event data) // Test feature 3: Log level filtering (log level is set to INFO) diff --git a/packages/logger/tests/e2e/basicFeatures.middy.test.ts b/packages/logger/tests/e2e/basicFeatures.middy.test.ts index 56d0c28b19..5cef007bbc 100644 --- a/packages/logger/tests/e2e/basicFeatures.middy.test.ts +++ b/packages/logger/tests/e2e/basicFeatures.middy.test.ts @@ -65,7 +65,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => { }); describe('Log level filtering', () => { - it('should filter log based on POWERTOOLS_LOG_LEVEL (INFO) environment variable in Lambda', async () => { + it('should filter log based on POWERTOOLS_LOG_LEVEL (INFO) environment variable in Lambda', () => { for (let i = 0; i < invocationCount; i++) { // Get log messages of the invocation and filter by level const debugLogs = invocationLogs[i].getFunctionLogs('DEBUG'); @@ -76,7 +76,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => { }); describe('Context data', () => { - it('should inject context info in each log', async () => { + it('should inject context info in each log', () => { for (let i = 0; i < invocationCount; i++) { // Get log messages of the invocation const logMessages = invocationLogs[i].getFunctionLogs(); @@ -92,7 +92,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => { } }); - it('should include coldStart equal to TRUE only on the first invocation, FALSE otherwise', async () => { + it('should include coldStart equal to TRUE only on the first invocation, FALSE otherwise', () => { for (let i = 0; i < invocationCount; i++) { // Get log messages of the invocation const logMessages = invocationLogs[i].getFunctionLogs(); @@ -109,7 +109,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => { }); }); - it('logs the event for every invocation, only once, and without keys from previous invocations', async () => { + it('logs the event for every invocation, only once, and without keys from previous invocations', () => { const { RUNTIME_ADDED_KEY: runtimeAddedKey } = commonEnvironmentVars; for (let i = 0; i < invocationCount; i++) { @@ -132,7 +132,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => { }); describe('Persistent additional log keys and values', () => { - it('should contain persistent value in every log', async () => { + it('should contain persistent value in every log', () => { const { PERSISTENT_KEY: persistentKey, PERSISTENT_VALUE: persistentValue, @@ -151,7 +151,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => { } }); - it('should not contain persistent keys that were removed on runtime', async () => { + it('should not contain persistent keys that were removed on runtime', () => { const { REMOVABLE_KEY: removableKey, REMOVABLE_VALUE: removableValue } = commonEnvironmentVars; @@ -176,7 +176,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => { }); describe('One-time additional log keys and values', () => { - it('should log additional keys and value only once', async () => { + it('should log additional keys and value only once', () => { const { SINGLE_LOG_ITEM_KEY: singleLogItemKey, SINGLE_LOG_ITEM_VALUE: singleLogItemValue, @@ -200,7 +200,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => { }); describe('Error logging', () => { - it('should log error only once', async () => { + it('should log error only once', () => { const { ERROR_MSG: errorMsg } = commonEnvironmentVars; for (let i = 0; i < invocationCount; i++) { @@ -226,7 +226,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => { }); describe('Arbitrary object logging', () => { - it('should log additional arbitrary object only once', async () => { + it('should log additional arbitrary object only once', () => { const { ARBITRARY_OBJECT_KEY: objectKey, ARBITRARY_OBJECT_DATA: objectData, @@ -263,7 +263,7 @@ describe('Logger E2E tests, basic functionalities middy usage', () => { }); describe('X-Ray Trace ID injection', () => { - it('should inject & parse the X-Ray Trace ID of the current invocation into every log', async () => { + it('should inject & parse the X-Ray Trace ID of the current invocation into every log', () => { for (let i = 0; i < invocationCount; i++) { // Get log messages of the invocation const logMessages = invocationLogs[i].getFunctionLogs(); diff --git a/packages/logger/tests/e2e/childLogger.manual.test.FunctionCode.ts b/packages/logger/tests/e2e/childLogger.manual.test.FunctionCode.ts index f7ba75b2a4..a01c6b8005 100644 --- a/packages/logger/tests/e2e/childLogger.manual.test.FunctionCode.ts +++ b/packages/logger/tests/e2e/childLogger.manual.test.FunctionCode.ts @@ -20,10 +20,10 @@ const childLogger = parentLogger.createChild({ logLevel: CHILD_LOG_LEVEL, }); -export const handler = async ( +export const handler = ( _event: TestEvent, context: Context -): TestOutput => { +): Awaited => { parentLogger.addContext(context); childLogger.info(CHILD_LOG_MSG); diff --git a/packages/logger/tests/e2e/childLogger.manual.test.ts b/packages/logger/tests/e2e/childLogger.manual.test.ts index cc90c4be84..a4a7953871 100644 --- a/packages/logger/tests/e2e/childLogger.manual.test.ts +++ b/packages/logger/tests/e2e/childLogger.manual.test.ts @@ -57,7 +57,7 @@ describe('Logger E2E tests, child logger', () => { }); describe('Child logger', () => { - it('should not log at same level of parent because of its own logLevel', async () => { + it('should not log at same level of parent because of its own logLevel', () => { const { PARENT_LOG_MSG: parentLogMsg, CHILD_LOG_MSG: childLogMsg } = commonEnvironmentVars; @@ -77,7 +77,7 @@ describe('Logger E2E tests, child logger', () => { } }); - it('should log only level passed to a child', async () => { + it('should log only level passed to a child', () => { const { CHILD_LOG_MSG: childLogMsg } = commonEnvironmentVars; for (let i = 0; i < invocationCount; i++) { // Get log messages of the invocation @@ -95,7 +95,7 @@ describe('Logger E2E tests, child logger', () => { } }); - it('should NOT inject context into the child logger', async () => { + it('should NOT inject context into the child logger', () => { const { CHILD_LOG_MSG: childLogMsg } = commonEnvironmentVars; for (let i = 0; i < invocationCount; i++) { @@ -118,7 +118,7 @@ describe('Logger E2E tests, child logger', () => { } }); - it('both logger instances should have the same persistent key/value', async () => { + it('both logger instances should have the same persistent key/value', () => { const { PERSISTENT_KEY: persistentKey } = commonEnvironmentVars; for (let i = 0; i < invocationCount; i++) { diff --git a/packages/logger/tests/e2e/logEventEnvVarSetting.middy.test.ts b/packages/logger/tests/e2e/logEventEnvVarSetting.middy.test.ts index 63d1ed86ad..b303fde3e1 100644 --- a/packages/logger/tests/e2e/logEventEnvVarSetting.middy.test.ts +++ b/packages/logger/tests/e2e/logEventEnvVarSetting.middy.test.ts @@ -60,7 +60,7 @@ describe('Logger E2E tests, log event via env var setting with middy', () => { }); describe('Log event', () => { - it('should log the event as the first log of each invocation only', async () => { + it('should log the event as the first log of each invocation only', () => { for (let i = 0; i < invocationCount; i++) { // Get log messages of the invocation const logMessages = invocationLogs[i].getFunctionLogs(); diff --git a/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts b/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts index b0cd540189..cf253ff8f8 100644 --- a/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts +++ b/packages/logger/tests/e2e/sampleRate.decorator.test.FunctionCode.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; import type { Context } from 'aws-lambda'; import { Logger } from '../../src/index.js'; @@ -16,6 +17,7 @@ class Lambda implements LambdaInterface { @logger.injectLambdaContext() public async handler(_event: TestEvent, context: Context): TestOutput { this.printLogInAllLevels(); + await setTimeout(1); // simulate some async work return { requestId: context.awsRequestId, diff --git a/packages/logger/tests/e2e/sampleRate.decorator.test.ts b/packages/logger/tests/e2e/sampleRate.decorator.test.ts index 2be3ea594a..27d888c3b1 100644 --- a/packages/logger/tests/e2e/sampleRate.decorator.test.ts +++ b/packages/logger/tests/e2e/sampleRate.decorator.test.ts @@ -59,7 +59,7 @@ describe('Logger E2E tests, sample rate and injectLambdaContext()', () => { }); describe('Enabling sample rate', () => { - it('should log all levels based on given sample rate, not just ERROR', async () => { + it('should log all levels based on given sample rate, not just ERROR', () => { // Fetch log streams from all invocations let countSampled = 0; let countNotSampled = 0; @@ -94,7 +94,7 @@ describe('Logger E2E tests, sample rate and injectLambdaContext()', () => { }); describe('Decorator injectLambdaContext()', () => { - it('should inject Lambda context into every log emitted', async () => { + it('should inject Lambda context into every log emitted', () => { for (let i = 0; i < invocationCount; i++) { // Get log messages of the invocation const logMessages = invocationLogs[i].getFunctionLogs('ERROR'); diff --git a/packages/logger/tests/helpers/resources.ts b/packages/logger/tests/helpers/resources.ts index e7db1993e4..ea811f8eea 100644 --- a/packages/logger/tests/helpers/resources.ts +++ b/packages/logger/tests/helpers/resources.ts @@ -5,7 +5,7 @@ import type { TestNodejsFunctionProps, } from '@aws-lambda-powertools/testing-utils/types'; import { CfnOutput } from 'aws-cdk-lib'; -import { commonEnvironmentVars } from '../e2e/constants'; +import { commonEnvironmentVars } from '../e2e/constants.js'; interface LoggerExtraTestProps extends ExtraTestProps { logGroupOutputKey?: string; diff --git a/packages/logger/tests/unit/injectLambdaContext.test.ts b/packages/logger/tests/unit/injectLambdaContext.test.ts index 48ad337693..4bc6bcec53 100644 --- a/packages/logger/tests/unit/injectLambdaContext.test.ts +++ b/packages/logger/tests/unit/injectLambdaContext.test.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import context from '@aws-lambda-powertools/testing-utils/context'; import middy from '@middy/core'; import type { Context } from 'aws-lambda'; @@ -78,7 +79,7 @@ describe('Inject Lambda Context', () => { it('adds the context to log messages when the feature is enabled in the Middy.js middleware', async () => { // Prepare const logger = new Logger(); - const handler = middy(async () => { + const handler = middy(() => { logger.info('Hello, world!'); }).use(injectLambdaContext(logger)); @@ -100,7 +101,7 @@ describe('Inject Lambda Context', () => { // Prepare const logger1 = new Logger({ serviceName: 'parent' }); const logger2 = logger1.createChild({ serviceName: 'child' }); - const handler = middy(async () => { + const handler = middy(() => { logger1.info('Hello, world!'); logger2.info('Hello, world!'); }).use(injectLambdaContext([logger1, logger2])); @@ -140,6 +141,7 @@ describe('Inject Lambda Context', () => { @logger.injectLambdaContext() async handler(_event: unknown, _context: Context) { + await setTimeout(1); // simulate some async operation this.logGreeting(); } @@ -189,7 +191,7 @@ describe('Inject Lambda Context', () => { { case: 'middleware', getHandler: (logger: Logger) => - middy(async () => { + middy(() => { logger.info('Hello, world!'); }).use(injectLambdaContext(logger)), }, @@ -202,6 +204,7 @@ describe('Inject Lambda Context', () => { _event: unknown, _context: Context ): Promise { + await setTimeout(1); // simulate some async operation logger.info('test'); } } @@ -245,6 +248,7 @@ describe('Inject Lambda Context', () => { case: 'middleware', getHandler: (logger: Logger) => middy(async () => { + await setTimeout(1); // simulate some async operation logger.info('Hello, world!'); }).use( injectLambdaContext(logger, { @@ -263,6 +267,7 @@ describe('Inject Lambda Context', () => { _event: unknown, _context: Context ): Promise { + await setTimeout(1); // simulate some async operation logger.info('Hello, world!'); } } @@ -306,7 +311,7 @@ describe('Inject Lambda Context', () => { }, }; // Act - const handler = middy(async () => { + const handler = middy(() => { logger.info('Hello, world!'); }).use( injectLambdaContext(logger, { @@ -329,7 +334,7 @@ describe('Inject Lambda Context', () => { const logger = new Logger({ correlationIdSearchFn: search }); // Act - Use middleware which will internally call setCorrelationIdFromPath - const handler = middy(async () => { + const handler = middy(() => { logger.info('Hello, world!'); }).use( injectLambdaContext(logger, { @@ -388,7 +393,7 @@ describe('Inject Lambda Context', () => { it('uses the API_GATEWAY_REST predefined path to extract correlation ID', async () => { // Prepare const logger = new Logger({ correlationIdSearchFn: search }); - const handler = middy(async () => { + const handler = middy(() => { logger.info('Using API Gateway request ID'); }).use( injectLambdaContext(logger, { @@ -419,7 +424,7 @@ describe('Inject Lambda Context', () => { { case: 'middleware', getHandler: (logger: Logger) => - middy(async (event: { id: number }) => { + middy((event: { id: number }) => { logger.info('Processing event'); logger.appendKeys({ id: event.id }); throw new Error('Test error'); @@ -440,6 +445,7 @@ describe('Inject Lambda Context', () => { event: { id: number }, _context: Context ): Promise { + await setTimeout(1); // simulate some async operation logger.info('Processing event'); logger.appendKeys({ id: event.id }); throw new Error('Test error'); @@ -480,7 +486,7 @@ describe('Inject Lambda Context', () => { { case: 'middleware', getHandler: (logger: Logger) => - middy(async (event: { id: number }) => { + middy((event: { id: number }) => { logger.info('Processing event'); logger.appendKeys({ id: event.id }); return true; @@ -498,6 +504,7 @@ describe('Inject Lambda Context', () => { resetKeys: true, }) public async handler(event: { id: number }, _context: Context) { + await setTimeout(1); // simulate some async operation logger.info('Processing event'); logger.appendKeys({ id: event.id }); return true; diff --git a/packages/logger/tests/unit/logBuffer.test.ts b/packages/logger/tests/unit/logBuffer.test.ts index ca1febdbce..c3d54133b5 100644 --- a/packages/logger/tests/unit/logBuffer.test.ts +++ b/packages/logger/tests/unit/logBuffer.test.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import context from '@aws-lambda-powertools/testing-utils/context'; import type { Context } from 'aws-lambda'; import middy from 'middy5'; @@ -283,7 +284,7 @@ describe('Buffer logs', () => { .use( injectLambdaContext(logger, { flushBufferOnUncaughtError: true }) ) - .handler(async () => { + .handler(() => { logger.debug('This is a log message'); logger.info('This is an info message'); throw new Error('This is an error'); @@ -297,6 +298,7 @@ describe('Buffer logs', () => { async handler(_event: unknown, _context: Context) { logger.debug('This is a log message'); logger.info('This is an info message'); + await setTimeout(1); // simulate some async operation throw new Error('This is an error'); } } @@ -342,7 +344,7 @@ describe('Buffer logs', () => { .use( injectLambdaContext(logger, { flushBufferOnUncaughtError: false }) ) - .handler(async () => { + .handler(() => { logger.debug('This is a log message'); logger.info('This is an info message'); throw new Error('This is an error'); @@ -356,6 +358,7 @@ describe('Buffer logs', () => { async handler(_event: unknown, _context: Context) { logger.debug('This is a log message'); logger.info('This is an info message'); + await setTimeout(1); // simulate some async operation throw new Error('This is an error'); } } diff --git a/packages/logger/tests/unit/logEvent.test.ts b/packages/logger/tests/unit/logEvent.test.ts index a694e96984..e0388ebeac 100644 --- a/packages/logger/tests/unit/logEvent.test.ts +++ b/packages/logger/tests/unit/logEvent.test.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import context from '@aws-lambda-powertools/testing-utils/context'; import middy from '@middy/core'; import type { Context } from 'aws-lambda'; @@ -86,6 +87,7 @@ describe('Log event', () => { class Test { @logger.injectLambdaContext({ logEvent: true }) async handler(event: unknown, _context: Context) { + await setTimeout(1); // simulate some async operation return event; } } diff --git a/packages/logger/tests/unit/setPowertoolsLogData.test.ts b/packages/logger/tests/unit/setPowertoolsLogData.test.ts deleted file mode 100644 index 696aec4438..0000000000 --- a/packages/logger/tests/unit/setPowertoolsLogData.test.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Logger } from '../../src/Logger.js'; -import { describe, it, expect, vi, afterEach } from 'vitest'; - -describe('Logger PowertoolsLogData environment variables', () => { - afterEach(() => { - vi.unstubAllEnvs(); - }); - - it.each([ - [undefined, ''], - ['us-west-2', 'us-west-2'], - ])('sets awsRegion to %s when AWS_REGION env var is %s', (envValue, expected) => { - // Prepare - vi.stubEnv('AWS_REGION', envValue); - // Act - const logger = new Logger(); - // Assess - expect(logger['powertoolsLogData'].awsRegion).toBe(expected); - }); - - it.each([ - [undefined, ''], - ['prd', 'prd'], - ])('sets environment to %s when ENVIRONMENT env var is %s', (envValue, expected) => { - // Prepare - vi.stubEnv('ENVIRONMENT', envValue); - // Act - const logger = new Logger(); - // Assess - expect(logger['powertoolsLogData'].environment).toBe(expected); - }); - - it.each([ - [undefined, 'service_undefined'], - ['my-service', 'my-service'], - ])('sets serviceName to %s when POWERTOOLS_SERVICE_NAME env var is %s', (envValue, expected) => { - // Prepare - vi.stubEnv('POWERTOOLS_SERVICE_NAME', envValue); - // Act - const logger = new Logger(); - // Assess - expect(logger['powertoolsLogData'].serviceName).toBe(expected); - }); -}); \ No newline at end of file diff --git a/packages/logger/tests/unit/workingWithkeys.test.ts b/packages/logger/tests/unit/workingWithkeys.test.ts index ca18259ef3..f921ef9b12 100644 --- a/packages/logger/tests/unit/workingWithkeys.test.ts +++ b/packages/logger/tests/unit/workingWithkeys.test.ts @@ -1,3 +1,4 @@ +import { setTimeout } from 'node:timers/promises'; import context from '@aws-lambda-powertools/testing-utils/context'; import middy from '@middy/core'; import type { Context } from 'aws-lambda'; @@ -439,7 +440,7 @@ describe('Working with keys', () => { foo: 'bar', }, }); - const handler = middy(async (addKey: boolean) => { + const handler = middy((addKey: boolean) => { if (addKey) { logger.appendKeys({ foo: 'baz', @@ -483,6 +484,7 @@ describe('Working with keys', () => { foo: 'baz', }); } + await setTimeout(1); // simulate some async operation logger.info('Hello, world!'); } }