44 * @group unit/metrics/class
55 */
66
7- import { ContextExamples as dummyContext , LambdaInterface } from '@aws-lambda-powertools/commons' ;
7+ import { ContextExamples as dummyContext , Events as dummyEvent , LambdaInterface } from '@aws-lambda-powertools/commons' ;
88import { Context , Callback } from 'aws-lambda' ;
9- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
10- // @ts -ignore
11- import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json' ;
129import { Metrics , MetricUnits } from '../../src/' ;
1310import { populateEnvironmentVariables } from '../helpers' ;
1411
@@ -22,14 +19,10 @@ interface LooseObject {
2219 [ key : string ] : string
2320}
2421
25- type DummyEvent = {
26- key1 : string
27- key2 : string
28- key3 : string
29- } ;
30-
3122describe ( 'Class: Metrics' , ( ) => {
3223 const originalEnvironmentVariables = process . env ;
24+ const context = dummyContext . helloworldContext ;
25+ const event = dummyEvent . Custom . CustomEvent ;
3326
3427 beforeEach ( ( ) => {
3528 consoleSpy . mockClear ( ) ;
@@ -99,7 +92,7 @@ describe('Class: Metrics', () => {
9992 }
10093 }
10194
102- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
95+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
10396 const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) , JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ] ;
10497
10598 expect ( console . log ) . toBeCalledTimes ( 2 ) ;
@@ -193,7 +186,7 @@ describe('Class: Metrics', () => {
193186 }
194187 }
195188
196- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
189+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
197190 const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) , JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ] ;
198191
199192 expect ( console . log ) . toBeCalledTimes ( 2 ) ;
@@ -227,7 +220,7 @@ describe('Class: Metrics', () => {
227220 }
228221 }
229222
230- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
223+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
231224 } catch ( e ) {
232225 expect ( ( < Error > e ) . message ) . toBe ( 'Max dimension count hit' ) ;
233226 }
@@ -255,7 +248,7 @@ describe('Class: Metrics', () => {
255248 }
256249 }
257250
258- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
251+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
259252 const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
260253
261254 expect ( console . log ) . toBeCalledTimes ( 1 ) ;
@@ -283,7 +276,7 @@ describe('Class: Metrics', () => {
283276 }
284277 }
285278
286- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
279+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
287280 const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
288281
289282 expect ( console . log ) . toBeCalledTimes ( 1 ) ;
@@ -297,13 +290,13 @@ describe('Class: Metrics', () => {
297290 test ( 'Cold start metric should only be written out once and flushed automatically' , async ( ) => {
298291 const metrics = new Metrics ( { namespace : 'test' } ) ;
299292
300- const handler = async ( _event : DummyEvent , _context : Context ) : Promise < void > => {
293+ const handler = async ( _event : unknown , _context : Context ) : Promise < void > => {
301294 // Should generate only one log
302295 metrics . captureColdStartMetric ( ) ;
303296 } ;
304297
305- await handler ( dummyEvent , dummyContext . helloworldContext ) ;
306- await handler ( dummyEvent , dummyContext . helloworldContext ) ;
298+ await handler ( event , context ) ;
299+ await handler ( event , context ) ;
307300 const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ] ;
308301
309302 expect ( console . log ) . toBeCalledTimes ( 1 ) ;
@@ -329,8 +322,8 @@ describe('Class: Metrics', () => {
329322 }
330323 }
331324
332- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
333- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked again!' ) ) ;
325+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
326+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked again!' ) ) ;
334327 const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) , JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ] ;
335328
336329 expect ( console . log ) . toBeCalledTimes ( 3 ) ;
@@ -356,15 +349,15 @@ describe('Class: Metrics', () => {
356349 metrics . addMetric ( 'test_name' , MetricUnits . Seconds , 10 ) ;
357350 }
358351 }
359- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
352+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
360353 const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
361354
362355 expect ( console . log ) . toBeCalledTimes ( 2 ) ;
363356 expect ( loggedData . _aws . CloudWatchMetrics [ 0 ] . Metrics . length ) . toBe ( 1 ) ;
364357 expect ( loggedData . _aws . CloudWatchMetrics [ 0 ] . Metrics [ 0 ] . Name ) . toBe ( 'ColdStart' ) ;
365358 expect ( loggedData . _aws . CloudWatchMetrics [ 0 ] . Metrics [ 0 ] . Unit ) . toBe ( 'Count' ) ;
366359 expect ( loggedData . service ) . toBe ( serviceName ) ;
367- expect ( loggedData . function_name ) . toBe ( dummyContext . helloworldContext . functionName ) ;
360+ expect ( loggedData . function_name ) . toBe ( context . functionName ) ;
368361 expect ( loggedData . _aws . CloudWatchMetrics [ 0 ] . Dimensions [ 0 ] ) . toContain ( 'service' ) ;
369362 expect ( loggedData . _aws . CloudWatchMetrics [ 0 ] . Dimensions [ 0 ] ) . toContain ( 'function_name' ) ;
370363 expect ( loggedData . ColdStart ) . toBe ( 1 ) ;
@@ -373,8 +366,8 @@ describe('Class: Metrics', () => {
373366 test ( 'Cold should still log, without a function name' , async ( ) => {
374367 const serviceName = 'test-service' ;
375368 const metrics = new Metrics ( { namespace : 'test' , serviceName : serviceName } ) ;
376- const newDummyContext = JSON . parse ( JSON . stringify ( dummyContext ) ) ;
377- delete newDummyContext . functionName ;
369+ const newContext = JSON . parse ( JSON . stringify ( context ) ) ;
370+ delete newContext . functionName ;
378371 class LambdaFunction implements LambdaInterface {
379372 @metrics . logMetrics ( { captureColdStartMetric : true } )
380373 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -388,7 +381,7 @@ describe('Class: Metrics', () => {
388381 }
389382 }
390383
391- await new LambdaFunction ( ) . handler ( dummyEvent , newDummyContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
384+ await new LambdaFunction ( ) . handler ( event , newContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
392385 const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
393386
394387 expect ( console . log ) . toBeCalledTimes ( 2 ) ;
@@ -421,7 +414,7 @@ describe('Class: Metrics', () => {
421414 }
422415
423416 try {
424- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
417+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
425418 } catch ( e ) {
426419 expect ( ( < Error > e ) . message ) . toBe ( 'The number of metrics recorded must be higher than zero' ) ;
427420 }
@@ -445,7 +438,7 @@ describe('Class: Metrics', () => {
445438 }
446439
447440 try {
448- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
441+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
449442 } catch ( e ) {
450443 fail ( `Should not throw but got the following Error: ${ e } ` ) ;
451444 }
@@ -457,14 +450,14 @@ describe('Class: Metrics', () => {
457450 expect . assertions ( 1 ) ;
458451
459452 const metrics = new Metrics ( { namespace : 'test' } ) ;
460- const handler = async ( _event : DummyEvent , _context : Context ) : Promise < void > => {
453+ const handler = async ( _event : unknown , _context : Context ) : Promise < void > => {
461454 metrics . throwOnEmptyMetrics ( ) ;
462455 // Logic goes here
463456 metrics . publishStoredMetrics ( ) ;
464457 } ;
465458
466459 try {
467- await handler ( dummyEvent , dummyContext . helloworldContext ) ;
460+ await handler ( event , context ) ;
468461 } catch ( e ) {
469462 expect ( ( < Error > e ) . message ) . toBe ( 'The number of metrics recorded must be higher than zero' ) ;
470463 }
@@ -490,7 +483,7 @@ describe('Class: Metrics', () => {
490483 }
491484 }
492485
493- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
486+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
494487 const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) , JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ] ;
495488
496489 expect ( console . log ) . toBeCalledTimes ( 2 ) ;
@@ -591,7 +584,7 @@ describe('Class: Metrics', () => {
591584 }
592585 }
593586
594- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
587+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
595588 } ) ;
596589
597590 test ( 'Publish Stored Metrics should log and clear' , async ( ) => {
@@ -610,7 +603,7 @@ describe('Class: Metrics', () => {
610603 }
611604 }
612605
613- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
606+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
614607 const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) , JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ] ;
615608
616609 expect ( console . log ) . toBeCalledTimes ( 2 ) ;
@@ -640,7 +633,7 @@ describe('Class: Metrics', () => {
640633 }
641634
642635 // Act
643- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext ) ;
636+ await new LambdaFunction ( ) . handler ( event , context ) ;
644637
645638 // Assess
646639 expect ( console . log ) . toBeCalledTimes ( 1 ) ;
@@ -669,7 +662,7 @@ describe('Class: Metrics', () => {
669662 }
670663 }
671664
672- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext ) ;
665+ await new LambdaFunction ( ) . handler ( event , context ) ;
673666 const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
674667
675668 expect ( console . log ) . toBeCalledTimes ( 1 ) ;
@@ -695,7 +688,7 @@ describe('Class: Metrics', () => {
695688 }
696689
697690 try {
698- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
691+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
699692 } catch ( error ) {
700693 // DO NOTHING
701694 }
@@ -724,7 +717,7 @@ describe('Class: Metrics', () => {
724717 metrics . addMetric ( 'test_name' , MetricUnits . Seconds , 1 ) ;
725718 }
726719 }
727- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
720+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
728721 const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
729722
730723 // Assess
0 commit comments