@@ -1227,11 +1227,10 @@ describe('Class: Logger', () => {
12271227 test ( 'when used as decorator on an async method, the method is awaited correctly' , async ( ) => {
12281228
12291229 // Prepare
1230- const injectLambdaContextAfterOrOnErrorMock = jest . fn ( ) . mockReturnValue ( 'worked' ) ;
1231- // Temporarily override the cleanup static method so that we can "spy" on it.
1232- // This method is always called after the handler has returned in the decorator
1233- // implementation.
1234- Logger . injectLambdaContextAfterOrOnError = injectLambdaContextAfterOrOnErrorMock ;
1230+ const injectLambdaContextAfterOrOnErrorSpy = jest . spyOn (
1231+ Logger ,
1232+ 'injectLambdaContextAfterOrOnError'
1233+ ) ;
12351234 const logger = new Logger ( {
12361235 logLevel : 'DEBUG' ,
12371236 } ) ;
@@ -1259,10 +1258,70 @@ describe('Class: Logger', () => {
12591258
12601259 // Assess
12611260 expect ( consoleSpy ) . toBeCalledTimes ( 1 ) ;
1262- // Here we assert that the logger.info method is called before the cleanup function that should awlays
1263- // be called after the handler has returned. If logger.info is called after it means the decorator is
1264- // NOT awaiting the handler which would cause the test to fail.
1265- expect ( consoleSpy . mock . invocationCallOrder [ 0 ] ) . toBeLessThan ( injectLambdaContextAfterOrOnErrorMock . mock . invocationCallOrder [ 0 ] ) ;
1261+ // Here we assert that the logger.info method is called before the cleanup function that should always
1262+ // be called ONLY after the handler has returned. If logger.info is called after the cleanup function
1263+ // it means the decorator is NOT awaiting the handler which would cause the test to fail.
1264+ expect ( consoleSpy . mock . invocationCallOrder [ 0 ] )
1265+ . toBeLessThan ( injectLambdaContextAfterOrOnErrorSpy . mock . invocationCallOrder [ 0 ] ) ;
1266+
1267+ } ) ;
1268+
1269+ test ( 'when logEvent and clearState are both TRUE, and the logger has persistent attributes, any key added in the handler is cleared properly' , async ( ) => {
1270+
1271+ // Prepare
1272+ const logger = new Logger ( {
1273+ persistentLogAttributes : {
1274+ version : '1.0.0' ,
1275+ }
1276+ } ) ;
1277+ const consoleSpy = jest . spyOn ( logger [ 'console' ] , 'info' ) . mockImplementation ( ) ;
1278+ class LambdaFunction implements LambdaInterface {
1279+ @logger . injectLambdaContext ( { clearState : true , logEvent : true } )
1280+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
1281+ // @ts -ignore
1282+ public async handler ( event : { foo : string } , _context : unknown ) : Promise < unknown > {
1283+ logger . appendKeys ( { foo : event . foo } ) ;
1284+
1285+ return ;
1286+ }
1287+ }
1288+ const lambda = new LambdaFunction ( ) ;
1289+ const handler = lambda . handler . bind ( lambda ) ;
1290+
1291+ // Act
1292+ await handler ( { foo : 'bar' } , { } as Context ) ;
1293+ await handler ( { foo : 'baz' } , { } as Context ) ;
1294+ await handler ( { foo : 'biz' } , { } as Context ) ;
1295+ await handler ( { foo : 'buz' } , { } as Context ) ;
1296+ await handler ( { foo : 'boz' } , { } as Context ) ;
1297+
1298+ expect ( consoleSpy ) . toBeCalledTimes ( 5 ) ;
1299+ for ( let i = 1 ; i === 5 ; i ++ ) {
1300+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1301+ i ,
1302+ expect . stringContaining ( '\"message\":\"Lambda invocation event\"' ) ,
1303+ ) ;
1304+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1305+ i ,
1306+ expect . stringContaining ( '\"version\":\"1.0.0\"' ) ,
1307+ ) ;
1308+ }
1309+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1310+ 2 ,
1311+ expect . not . stringContaining ( '\"foo\":\"bar\"' )
1312+ ) ;
1313+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1314+ 3 ,
1315+ expect . not . stringContaining ( '\"foo\":\"baz\"' )
1316+ ) ;
1317+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1318+ 4 ,
1319+ expect . not . stringContaining ( '\"foo\":\"biz\"' )
1320+ ) ;
1321+ expect ( consoleSpy ) . toHaveBeenNthCalledWith (
1322+ 5 ,
1323+ expect . not . stringContaining ( '\"foo\":\"buz\"' )
1324+ ) ;
12661325
12671326 } ) ;
12681327
0 commit comments