@@ -11,34 +11,16 @@ import {
1111} from '../../../../metrics/src' ;
1212import middy from '@middy/core' ;
1313import { ExtraOptions } from '../../../src/types' ;
14+ import { cleanupMiddlewares } from '@aws-lambda-powertools/commons/lib/middleware' ;
15+ import { helloworldContext as dummyContext } from '../../../../commons/src/samples/resources/contexts/hello-world' ;
16+ import { CustomEvent as dummyEvent } from '../../../../commons/src/samples/resources/events/custom/index' ;
1417
1518const consoleSpy = jest . spyOn ( console , 'log' ) . mockImplementation ( ) ;
1619const consoleWarnSpy = jest . spyOn ( console , 'warn' ) . mockImplementation ( ) ;
1720const mockDate = new Date ( 1466424490000 ) ;
1821jest . spyOn ( global , 'Date' ) . mockImplementation ( ( ) => mockDate ) ;
1922
2023describe ( 'Middy middleware' , ( ) => {
21- const dummyEvent = {
22- key1 : 'value1' ,
23- key2 : 'value2' ,
24- key3 : 'value3' ,
25- } ;
26- const dummyContext = {
27- callbackWaitsForEmptyEventLoop : true ,
28- functionVersion : '$LATEST' ,
29- functionName : 'foo-bar-function' ,
30- memoryLimitInMB : '128' ,
31- logGroupName : '/aws/lambda/foo-bar-function-123456abcdef' ,
32- logStreamName : '2021/03/09/[$LATEST]abcdef123456abcdef123456abcdef123456' ,
33- invokedFunctionArn :
34- 'arn:aws:lambda:eu-west-1:123456789012:function:Example' ,
35- awsRequestId : 'c6af9ac6-7b61-11e6-9a41-93e812345678' ,
36- getRemainingTimeInMillis : ( ) => 1234 ,
37- done : ( ) => console . log ( 'Done!' ) ,
38- fail : ( ) => console . log ( 'Failed!' ) ,
39- succeed : ( ) => console . log ( 'Succeeded!' ) ,
40- } ;
41-
4224 beforeEach ( ( ) => {
4325 jest . resetModules ( ) ;
4426 jest . clearAllMocks ( ) ;
@@ -399,6 +381,50 @@ describe('Middy middleware', () => {
399381 } )
400382 ) ;
401383 } ) ;
384+
385+ test ( 'when enabled, and another middleware returns early, it still publishes the metrics at the end of the execution' , async ( ) => {
386+ // Prepare
387+ const metrics = new Metrics ( {
388+ namespace : 'serverlessAirline' ,
389+ serviceName : 'orders' ,
390+ } ) ;
391+ const publishStoredMetricsSpy = jest . spyOn (
392+ metrics ,
393+ 'publishStoredMetrics'
394+ ) ;
395+ const myCustomMiddleware = ( ) : middy . MiddlewareObj => {
396+ const before = async (
397+ request : middy . Request
398+ ) : Promise < undefined | string > => {
399+ // Return early on the second invocation
400+ if ( request . event . idx === 1 ) {
401+ // Cleanup Powertools resources
402+ await cleanupMiddlewares ( request ) ;
403+
404+ // Then return early
405+ return 'foo' ;
406+ }
407+ } ;
408+
409+ return {
410+ before,
411+ } ;
412+ } ;
413+ const handler = middy (
414+ ( _event : typeof dummyEvent & { idx : number } ) : void => {
415+ metrics . addMetric ( 'successfulBooking' , MetricUnits . Count , 1 ) ;
416+ }
417+ )
418+ . use ( logMetrics ( metrics ) )
419+ . use ( myCustomMiddleware ( ) ) ;
420+
421+ // Act
422+ await handler ( { ...dummyEvent , idx : 0 } , dummyContext ) ;
423+ await handler ( { ...dummyEvent , idx : 1 } , dummyContext ) ;
424+
425+ // Assess
426+ expect ( publishStoredMetricsSpy ) . toBeCalledTimes ( 2 ) ;
427+ } ) ;
402428 } ) ;
403429 describe ( 'Metrics resolution' , ( ) => {
404430 test ( 'serialized metrics in EMF format should not contain `StorageResolution` as key if `60` is set' , async ( ) => {
0 commit comments