4
4
* @group unit/metrics/class
5
5
*/
6
6
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' ;
8
8
import { 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' ;
12
9
import { Metrics , MetricUnits } from '../../src/' ;
13
10
import { populateEnvironmentVariables } from '../helpers' ;
14
11
@@ -22,14 +19,10 @@ interface LooseObject {
22
19
[ key : string ] : string
23
20
}
24
21
25
- type DummyEvent = {
26
- key1 : string
27
- key2 : string
28
- key3 : string
29
- } ;
30
-
31
22
describe ( 'Class: Metrics' , ( ) => {
32
23
const originalEnvironmentVariables = process . env ;
24
+ const context = dummyContext . helloworldContext ;
25
+ const event = dummyEvent . Custom . CustomEvent ;
33
26
34
27
beforeEach ( ( ) => {
35
28
consoleSpy . mockClear ( ) ;
@@ -99,7 +92,7 @@ describe('Class: Metrics', () => {
99
92
}
100
93
}
101
94
102
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
95
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
103
96
const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) , JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ] ;
104
97
105
98
expect ( console . log ) . toBeCalledTimes ( 2 ) ;
@@ -193,7 +186,7 @@ describe('Class: Metrics', () => {
193
186
}
194
187
}
195
188
196
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
189
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
197
190
const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) , JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ] ;
198
191
199
192
expect ( console . log ) . toBeCalledTimes ( 2 ) ;
@@ -227,7 +220,7 @@ describe('Class: Metrics', () => {
227
220
}
228
221
}
229
222
230
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
223
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
231
224
} catch ( e ) {
232
225
expect ( ( < Error > e ) . message ) . toBe ( 'Max dimension count hit' ) ;
233
226
}
@@ -255,7 +248,7 @@ describe('Class: Metrics', () => {
255
248
}
256
249
}
257
250
258
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
251
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
259
252
const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
260
253
261
254
expect ( console . log ) . toBeCalledTimes ( 1 ) ;
@@ -283,7 +276,7 @@ describe('Class: Metrics', () => {
283
276
}
284
277
}
285
278
286
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
279
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
287
280
const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
288
281
289
282
expect ( console . log ) . toBeCalledTimes ( 1 ) ;
@@ -297,13 +290,13 @@ describe('Class: Metrics', () => {
297
290
test ( 'Cold start metric should only be written out once and flushed automatically' , async ( ) => {
298
291
const metrics = new Metrics ( { namespace : 'test' } ) ;
299
292
300
- const handler = async ( _event : DummyEvent , _context : Context ) : Promise < void > => {
293
+ const handler = async ( _event : unknown , _context : Context ) : Promise < void > => {
301
294
// Should generate only one log
302
295
metrics . captureColdStartMetric ( ) ;
303
296
} ;
304
297
305
- await handler ( dummyEvent , dummyContext . helloworldContext ) ;
306
- await handler ( dummyEvent , dummyContext . helloworldContext ) ;
298
+ await handler ( event , context ) ;
299
+ await handler ( event , context ) ;
307
300
const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ] ;
308
301
309
302
expect ( console . log ) . toBeCalledTimes ( 1 ) ;
@@ -329,8 +322,8 @@ describe('Class: Metrics', () => {
329
322
}
330
323
}
331
324
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!' ) ) ;
334
327
const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) , JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ] ;
335
328
336
329
expect ( console . log ) . toBeCalledTimes ( 3 ) ;
@@ -356,15 +349,15 @@ describe('Class: Metrics', () => {
356
349
metrics . addMetric ( 'test_name' , MetricUnits . Seconds , 10 ) ;
357
350
}
358
351
}
359
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
352
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
360
353
const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
361
354
362
355
expect ( console . log ) . toBeCalledTimes ( 2 ) ;
363
356
expect ( loggedData . _aws . CloudWatchMetrics [ 0 ] . Metrics . length ) . toBe ( 1 ) ;
364
357
expect ( loggedData . _aws . CloudWatchMetrics [ 0 ] . Metrics [ 0 ] . Name ) . toBe ( 'ColdStart' ) ;
365
358
expect ( loggedData . _aws . CloudWatchMetrics [ 0 ] . Metrics [ 0 ] . Unit ) . toBe ( 'Count' ) ;
366
359
expect ( loggedData . service ) . toBe ( serviceName ) ;
367
- expect ( loggedData . function_name ) . toBe ( dummyContext . helloworldContext . functionName ) ;
360
+ expect ( loggedData . function_name ) . toBe ( context . functionName ) ;
368
361
expect ( loggedData . _aws . CloudWatchMetrics [ 0 ] . Dimensions [ 0 ] ) . toContain ( 'service' ) ;
369
362
expect ( loggedData . _aws . CloudWatchMetrics [ 0 ] . Dimensions [ 0 ] ) . toContain ( 'function_name' ) ;
370
363
expect ( loggedData . ColdStart ) . toBe ( 1 ) ;
@@ -373,8 +366,8 @@ describe('Class: Metrics', () => {
373
366
test ( 'Cold should still log, without a function name' , async ( ) => {
374
367
const serviceName = 'test-service' ;
375
368
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 ;
378
371
class LambdaFunction implements LambdaInterface {
379
372
@metrics . logMetrics ( { captureColdStartMetric : true } )
380
373
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -388,7 +381,7 @@ describe('Class: Metrics', () => {
388
381
}
389
382
}
390
383
391
- await new LambdaFunction ( ) . handler ( dummyEvent , newDummyContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
384
+ await new LambdaFunction ( ) . handler ( event , newContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
392
385
const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
393
386
394
387
expect ( console . log ) . toBeCalledTimes ( 2 ) ;
@@ -421,7 +414,7 @@ describe('Class: Metrics', () => {
421
414
}
422
415
423
416
try {
424
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
417
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
425
418
} catch ( e ) {
426
419
expect ( ( < Error > e ) . message ) . toBe ( 'The number of metrics recorded must be higher than zero' ) ;
427
420
}
@@ -445,7 +438,7 @@ describe('Class: Metrics', () => {
445
438
}
446
439
447
440
try {
448
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
441
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
449
442
} catch ( e ) {
450
443
fail ( `Should not throw but got the following Error: ${ e } ` ) ;
451
444
}
@@ -457,14 +450,14 @@ describe('Class: Metrics', () => {
457
450
expect . assertions ( 1 ) ;
458
451
459
452
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 > => {
461
454
metrics . throwOnEmptyMetrics ( ) ;
462
455
// Logic goes here
463
456
metrics . publishStoredMetrics ( ) ;
464
457
} ;
465
458
466
459
try {
467
- await handler ( dummyEvent , dummyContext . helloworldContext ) ;
460
+ await handler ( event , context ) ;
468
461
} catch ( e ) {
469
462
expect ( ( < Error > e ) . message ) . toBe ( 'The number of metrics recorded must be higher than zero' ) ;
470
463
}
@@ -490,7 +483,7 @@ describe('Class: Metrics', () => {
490
483
}
491
484
}
492
485
493
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
486
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
494
487
const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) , JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ] ;
495
488
496
489
expect ( console . log ) . toBeCalledTimes ( 2 ) ;
@@ -591,7 +584,7 @@ describe('Class: Metrics', () => {
591
584
}
592
585
}
593
586
594
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
587
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
595
588
} ) ;
596
589
597
590
test ( 'Publish Stored Metrics should log and clear' , async ( ) => {
@@ -610,7 +603,7 @@ describe('Class: Metrics', () => {
610
603
}
611
604
}
612
605
613
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
606
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
614
607
const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) , JSON . parse ( consoleSpy . mock . calls [ 1 ] [ 0 ] ) ] ;
615
608
616
609
expect ( console . log ) . toBeCalledTimes ( 2 ) ;
@@ -640,7 +633,7 @@ describe('Class: Metrics', () => {
640
633
}
641
634
642
635
// Act
643
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext ) ;
636
+ await new LambdaFunction ( ) . handler ( event , context ) ;
644
637
645
638
// Assess
646
639
expect ( console . log ) . toBeCalledTimes ( 1 ) ;
@@ -669,7 +662,7 @@ describe('Class: Metrics', () => {
669
662
}
670
663
}
671
664
672
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext ) ;
665
+ await new LambdaFunction ( ) . handler ( event , context ) ;
673
666
const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
674
667
675
668
expect ( console . log ) . toBeCalledTimes ( 1 ) ;
@@ -695,7 +688,7 @@ describe('Class: Metrics', () => {
695
688
}
696
689
697
690
try {
698
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
691
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
699
692
} catch ( error ) {
700
693
// DO NOTHING
701
694
}
@@ -724,7 +717,7 @@ describe('Class: Metrics', () => {
724
717
metrics . addMetric ( 'test_name' , MetricUnits . Seconds , 1 ) ;
725
718
}
726
719
}
727
- await new LambdaFunction ( ) . handler ( dummyEvent , dummyContext . helloworldContext , ( ) => console . log ( 'Lambda invoked!' ) ) ;
720
+ await new LambdaFunction ( ) . handler ( event , context , ( ) => console . log ( 'Lambda invoked!' ) ) ;
728
721
const loggedData = JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ;
729
722
730
723
// Assess
0 commit comments