@@ -225,6 +225,25 @@ describe('Class: Metrics', () => {
225225 } ) ;
226226
227227 describe ( 'Feature: Cold Start' , ( ) => {
228+ test ( 'Cold start metric should only be written out once and flushed automatically' , async ( ) => {
229+ const metrics = new Metrics ( { namespace : 'test' } ) ;
230+
231+ const handler = async ( event : any , context : Context ) => {
232+ // Should generate only one log
233+ metrics . captureColdStartMetric ( ) ;
234+ } ;
235+
236+ await handler ( dummyEvent , dummyContext ) ;
237+ await handler ( dummyEvent , dummyContext ) ;
238+ const loggedData = [ JSON . parse ( consoleSpy . mock . calls [ 0 ] [ 0 ] ) ] ;
239+
240+ expect ( console . log ) . toBeCalledTimes ( 1 ) ;
241+ expect ( loggedData [ 0 ] . _aws . CloudWatchMetrics [ 0 ] . Metrics . length ) . toBe ( 1 ) ;
242+ expect ( loggedData [ 0 ] . _aws . CloudWatchMetrics [ 0 ] . Metrics [ 0 ] . Name ) . toBe ( 'ColdStart' ) ;
243+ expect ( loggedData [ 0 ] . _aws . CloudWatchMetrics [ 0 ] . Metrics [ 0 ] . Unit ) . toBe ( 'Count' ) ;
244+ expect ( loggedData [ 0 ] . ColdStart ) . toBe ( 1 ) ;
245+ } ) ;
246+
228247 test ( 'Cold start metric should only be written out once' , async ( ) => {
229248 const metrics = new Metrics ( { namespace : 'test' } ) ;
230249
@@ -338,6 +357,23 @@ describe('Class: Metrics', () => {
338357 expect ( ( < Error > e ) . message ) . toBe ( 'The number of metrics recorded must be higher than zero' ) ;
339358 }
340359 } ) ;
360+
361+ test ( 'Error should be thrown on empty metrics when raiseOnEmptyMetrics() is callse' , async ( ) => {
362+ expect . assertions ( 1 ) ;
363+
364+ const metrics = new Metrics ( { namespace : 'test' } ) ;
365+ const handler = async ( event : any , context : Context ) => {
366+ metrics . raiseOnEmptyMetrics ( ) ;
367+ // Logic goes here
368+ metrics . purgeStoredMetrics ( ) ;
369+ } ;
370+
371+ try {
372+ await handler ( dummyEvent , dummyContext ) ;
373+ } catch ( e ) {
374+ expect ( ( < Error > e ) . message ) . toBe ( 'The number of metrics recorded must be higher than zero' ) ;
375+ }
376+ } ) ;
341377 } ) ;
342378
343379 describe ( 'Feature: Auto log at limit' , ( ) => {
0 commit comments