@@ -552,4 +552,104 @@ describe('Working with dimensions', () => {
552552 } )
553553 ) ;
554554 } ) ;
555+
556+ it ( 'warns when setDefaultDimensions overwrites existing dimensions' , ( ) => {
557+ // Prepare
558+ const metrics = new Metrics ( {
559+ namespace : DEFAULT_NAMESPACE ,
560+ defaultDimensions : { environment : 'prod' } ,
561+ } ) ;
562+
563+ // Act
564+ metrics . setDefaultDimensions ( { region : 'us-east-1' } ) ;
565+ metrics . setDefaultDimensions ( {
566+ environment : 'staging' , // overwrites default dimension
567+ } ) ;
568+
569+ // Assess
570+ expect ( console . warn ) . toHaveBeenCalledOnce ( ) ;
571+ expect ( console . warn ) . toHaveBeenCalledWith (
572+ 'Dimension "environment" has already been added. The previous value will be overwritten.'
573+ ) ;
574+ } ) ;
575+
576+ it ( 'returns immediately if dimensions is undefined' , ( ) => {
577+ // Prepare
578+ const metrics = new Metrics ( {
579+ singleMetric : true ,
580+ namespace : DEFAULT_NAMESPACE ,
581+ } ) ;
582+
583+ // Act
584+ metrics . addMetric ( 'myMetric' , MetricUnit . Count , 1 ) ;
585+
586+ // Assert
587+ expect ( console . warn ) . not . toHaveBeenCalled ( ) ;
588+
589+ expect ( console . log ) . toHaveEmittedEMFWith (
590+ expect . objectContaining ( {
591+ service : 'hello-world' ,
592+ } )
593+ ) ;
594+ } ) ;
595+
596+ it . each ( [
597+ { value : undefined , name : 'valid-name' } ,
598+ { value : null , name : 'valid-name' } ,
599+ { value : '' , name : 'valid-name' } ,
600+ { value : 'valid-value' , name : '' } ,
601+ ] ) (
602+ 'skips invalid default dimension values in setDefaultDimensions ($name)' ,
603+ ( { value, name } ) => {
604+ // Arrange
605+ const metrics = new Metrics ( {
606+ singleMetric : true ,
607+ namespace : DEFAULT_NAMESPACE ,
608+ } ) ;
609+
610+ // Act
611+ metrics . setDefaultDimensions ( {
612+ validDimension : 'valid' ,
613+ [ name as string ] : value as string ,
614+ } ) ;
615+
616+ metrics . addMetric ( 'test' , MetricUnit . Count , 1 ) ;
617+ metrics . publishStoredMetrics ( ) ;
618+
619+ // Assert
620+ expect ( console . warn ) . toHaveBeenCalledWith (
621+ `The dimension ${ name } doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
622+ ) ;
623+
624+ expect ( console . log ) . toHaveEmittedEMFWith (
625+ expect . objectContaining ( { validDimension : 'valid' } )
626+ ) ;
627+
628+ expect ( console . log ) . toHaveEmittedEMFWith (
629+ expect . not . objectContaining ( { [ name ] : value } )
630+ ) ;
631+ }
632+ ) ;
633+ it ( 'returns immediately without logging if dimensions is not a plain object' , ( ) => {
634+ // Prepare
635+ const metrics = new Metrics ( {
636+ singleMetric : true ,
637+ namespace : DEFAULT_NAMESPACE ,
638+ } ) ;
639+
640+ // Act
641+ // @ts -expect-error – simulate runtime misuse
642+ metrics . setDefaultDimensions ( 'not-an-object' ) ;
643+
644+ // Assert
645+ expect ( console . warn ) . not . toHaveBeenCalled ( ) ;
646+
647+ metrics . addMetric ( 'someMetric' , MetricUnit . Count , 1 ) ;
648+
649+ expect ( console . log ) . toHaveEmittedEMFWith (
650+ expect . objectContaining ( {
651+ service : 'hello-world' ,
652+ } )
653+ ) ;
654+ } ) ;
555655} ) ;
0 commit comments