@@ -1235,11 +1235,10 @@ describe('Class: Logger', () => {
1235
1235
test ( 'it awaits the decorated method correctly' , async ( ) => {
1236
1236
1237
1237
// Prepare
1238
- const injectLambdaContextAfterOrOnErrorMock = jest . fn ( ) . mockReturnValue ( 'worked' ) ;
1239
- // Temporarily override the cleanup static method so that we can "spy" on it.
1240
- // This method is always called after the handler has returned in the decorator
1241
- // implementation.
1242
- Logger . injectLambdaContextAfterOrOnError = injectLambdaContextAfterOrOnErrorMock ;
1238
+ const injectLambdaContextAfterOrOnErrorSpy = jest . spyOn (
1239
+ Logger ,
1240
+ 'injectLambdaContextAfterOrOnError'
1241
+ ) ;
1243
1242
const logger = new Logger ( {
1244
1243
logLevel : 'DEBUG' ,
1245
1244
} ) ;
@@ -1248,7 +1247,7 @@ describe('Class: Logger', () => {
1248
1247
@logger . injectLambdaContext ( )
1249
1248
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
1250
1249
// @ts -ignore
1251
- public async handler < TResult > ( _event : unknown , _context : Context , _callback : Callback < TResult > ) : void | Promise < TResult > {
1250
+ public async handler ( _event : unknown , _context : unknown ) : Promise < unknown > {
1252
1251
await this . dummyMethod ( ) ;
1253
1252
logger . info ( 'This is a DEBUG log' ) ;
1254
1253
@@ -1259,18 +1258,78 @@ describe('Class: Logger', () => {
1259
1258
return ;
1260
1259
}
1261
1260
}
1262
-
1263
- // Act
1264
1261
const lambda = new LambdaFunction ( ) ;
1265
1262
const handler = lambda . handler . bind ( lambda ) ;
1266
- await handler ( { } , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
1263
+
1264
+ // Act
1265
+ await handler ( { } , context ) ;
1267
1266
1268
1267
// Assess
1269
1268
expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
1270
- // Here we assert that the logger.info method is called before the cleanup function that should awlays
1271
- // be called after the handler has returned. If logger.info is called after it means the decorator is
1272
- // NOT awaiting the handler which would cause the test to fail.
1273
- expect ( consoleSpy . mock . invocationCallOrder [ 0 ] ) . toBeLessThan ( injectLambdaContextAfterOrOnErrorMock . mock . invocationCallOrder [ 0 ] ) ;
1269
+ // Here we assert that the logger.info method is called before the cleanup function that should always
1270
+ // be called ONLY after the handler has returned. If logger.info is called after the cleanup function
1271
+ // it means the decorator is NOT awaiting the handler which would cause the test to fail.
1272
+ expect ( consoleSpy . mock . invocationCallOrder [ 0 ] )
1273
+ . toBeLessThan ( injectLambdaContextAfterOrOnErrorSpy . mock . invocationCallOrder [ 0 ] ) ;
1274
+
1275
+ } ) ;
1276
+
1277
+ test ( 'when logEvent and clearState are both TRUE, and the logger has persistent attributes, any key added in the handler is cleared properly' , async ( ) => {
1278
+
1279
+ // Prepare
1280
+ const logger = new Logger ( {
1281
+ persistentLogAttributes : {
1282
+ version : '1.0.0' ,
1283
+ }
1284
+ } ) ;
1285
+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , 'info' ) . mockImplementation ( ) ;
1286
+ class LambdaFunction implements LambdaInterface {
1287
+ @logger . injectLambdaContext ( { clearState : true , logEvent : true } )
1288
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1289
+ // @ts -ignore
1290
+ public async handler ( event : { foo : string } , _context : unknown ) : Promise < unknown > {
1291
+ logger . appendKeys ( { foo : event . foo } ) ;
1292
+
1293
+ return ;
1294
+ }
1295
+ }
1296
+ const lambda = new LambdaFunction ( ) ;
1297
+ const handler = lambda . handler . bind ( lambda ) ;
1298
+
1299
+ // Act
1300
+ await handler ( { foo : 'bar' } , { } as Context ) ;
1301
+ await handler ( { foo : 'baz' } , { } as Context ) ;
1302
+ await handler ( { foo : 'biz' } , { } as Context ) ;
1303
+ await handler ( { foo : 'buz' } , { } as Context ) ;
1304
+ await handler ( { foo : 'boz' } , { } as Context ) ;
1305
+
1306
+ expect ( consoleSpy ) . toBeCalledTimes ( 5 ) ;
1307
+ for ( let i = 1 ; i === 5 ; i ++ ) {
1308
+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1309
+ i ,
1310
+ expect . stringContaining ( '\"message\":\"Lambda invocation event\"' ) ,
1311
+ ) ;
1312
+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1313
+ i ,
1314
+ expect . stringContaining ( '\"version\":\"1.0.0\"' ) ,
1315
+ ) ;
1316
+ }
1317
+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1318
+ 2 ,
1319
+ expect . not . stringContaining ( '\"foo\":\"bar\"' )
1320
+ ) ;
1321
+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1322
+ 3 ,
1323
+ expect . not . stringContaining ( '\"foo\":\"baz\"' )
1324
+ ) ;
1325
+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1326
+ 4 ,
1327
+ expect . not . stringContaining ( '\"foo\":\"biz\"' )
1328
+ ) ;
1329
+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1330
+ 5 ,
1331
+ expect . not . stringContaining ( '\"foo\":\"buz\"' )
1332
+ ) ;
1274
1333
1275
1334
} ) ;
1276
1335
0 commit comments