@@ -918,6 +918,12 @@ describe('API tests', () => {
918918 } )
919919
920920 describe ( 'measure' , ( ) => {
921+ beforeAll ( ( ) => {
922+ global . PerformanceMark = function ( name , options ) {
923+ this . name = name
924+ this . startTime = options . startTime
925+ }
926+ } )
921927 test ( 'should create event emitter event for calls to API' , ( ) => {
922928 agent . measure ( 'testMeasure' )
923929
@@ -963,6 +969,134 @@ describe('API tests', () => {
963969 expect ( console . debug ) . toHaveBeenCalledTimes ( 1 )
964970 expect ( console . debug ) . toHaveBeenCalledWith ( expect . stringContaining ( 'New Relic Warning: https://github.com/newrelic/newrelic-browser-agent/blob/main/docs/warning-codes.md#58' ) , undefined )
965971 } )
972+
973+ describe ( 'should create correct output' , ( ) => {
974+ let dummyMark
975+ beforeAll ( ( ) => {
976+ dummyMark = ( name , startTime ) => new PerformanceMark ( name , { startTime } )
977+ } )
978+ test ( 'no arguments' , ( ) => {
979+ jest . spyOn ( global . performance , 'now' ) . mockReturnValue ( 12345 )
980+
981+ const measurements = agent . measure ( 'testMeasure' )
982+ expect ( measurements ) . toEqual ( {
983+ start : 0 ,
984+ end : 12345 ,
985+ duration : 12345 - 0 ,
986+ customAttributes : { }
987+ } )
988+ } )
989+
990+ test ( 'start - number, end undefined' , ( ) => {
991+ jest . spyOn ( global . performance , 'now' ) . mockReturnValue ( 12345 )
992+ const measurements = agent . measure ( 'testMeasure' , { start : 1000 } )
993+ expect ( measurements ) . toEqual ( {
994+ start : 1000 ,
995+ end : 12345 ,
996+ duration : 12345 - 1000 ,
997+ customAttributes : { }
998+ } )
999+ } )
1000+
1001+ test ( 'start - number, end - null' , ( ) => {
1002+ jest . spyOn ( global . performance , 'now' ) . mockReturnValue ( 12345 )
1003+ const measurements = agent . measure ( 'testMeasure' , { start : 1000 , end : null } )
1004+ expect ( measurements ) . toEqual ( {
1005+ start : 1000 ,
1006+ end : 12345 ,
1007+ duration : 12345 - 1000 ,
1008+ customAttributes : { }
1009+ } )
1010+ } )
1011+
1012+ test ( 'start - PerformanceMark, end - undefined' , ( ) => {
1013+ jest . spyOn ( global . performance , 'now' ) . mockReturnValue ( 12345 )
1014+
1015+ const measurements = agent . measure ( 'testMeasure' , { start : dummyMark ( 'startMark' , 1000 ) } )
1016+ expect ( measurements ) . toEqual ( {
1017+ start : 1000 ,
1018+ end : 12345 ,
1019+ duration : 12345 - 1000 ,
1020+ customAttributes : { }
1021+ } )
1022+ } )
1023+
1024+ test ( 'start - undefined, end - number' , ( ) => {
1025+ const measurements = agent . measure ( 'testMeasure' , { end : 1000 } )
1026+ expect ( measurements ) . toEqual ( {
1027+ start : 0 ,
1028+ end : 1000 ,
1029+ duration : 1000 - 0 ,
1030+ customAttributes : { }
1031+ } )
1032+ } )
1033+
1034+ test ( 'start - null, end - number' , ( ) => {
1035+ const measurements = agent . measure ( 'testMeasure' , { start : null , end : 1000 } )
1036+ expect ( measurements ) . toEqual ( {
1037+ start : 0 ,
1038+ end : 1000 ,
1039+ duration : 1000 - 0 ,
1040+ customAttributes : { }
1041+ } )
1042+ } )
1043+
1044+ test ( 'start - undefined, end - PerformanceMark' , ( ) => {
1045+ const measurements = agent . measure ( 'testMeasure' , { end : dummyMark ( 'endMark' , 1000 ) } )
1046+ expect ( measurements ) . toEqual ( {
1047+ start : 0 ,
1048+ end : 1000 ,
1049+ duration : 1000 - 0 ,
1050+ customAttributes : { }
1051+ } )
1052+ } )
1053+
1054+ test ( 'start - undefined, end - number' , ( ) => {
1055+ const measurements = agent . measure ( 'testMeasure' , { end : 1000 } )
1056+ expect ( measurements ) . toEqual ( {
1057+ start : 0 ,
1058+ end : 1000 ,
1059+ duration : 1000 - 0 ,
1060+ customAttributes : { }
1061+ } )
1062+ } )
1063+
1064+ test ( 'start - PerformanceMark, end - PerformanceMark' , ( ) => {
1065+ const measurements = agent . measure ( 'testMeasure' , { start : dummyMark ( 'startMark' , 1000 ) , end : dummyMark ( 'endMark' , 2000 ) } )
1066+ expect ( measurements ) . toEqual ( {
1067+ start : 1000 ,
1068+ end : 2000 ,
1069+ duration : 2000 - 1000 ,
1070+ customAttributes : { }
1071+ } )
1072+ } )
1073+
1074+ test ( 'start - number, end - PerformanceMark' , ( ) => {
1075+ const measurements = agent . measure ( 'testMeasure' , { start : 1000 , end : dummyMark ( 'endMark' , 2000 ) } )
1076+ expect ( measurements ) . toEqual ( {
1077+ start : 1000 ,
1078+ end : 2000 ,
1079+ duration : 2000 - 1000 ,
1080+ customAttributes : { }
1081+ } )
1082+ } )
1083+
1084+ test ( 'start - PerformanceMark, end - number' , ( ) => {
1085+ const measurements = agent . measure ( 'testMeasure' , { start : dummyMark ( 'startMark' , 1000 ) , end : 2000 } )
1086+ expect ( measurements ) . toEqual ( {
1087+ start : 1000 ,
1088+ end : 2000 ,
1089+ duration : 2000 - 1000 ,
1090+ customAttributes : { }
1091+ } )
1092+ } )
1093+
1094+ test ( 'custom attributes' , ( ) => {
1095+ const measurements = agent . measure ( 'testMeasure' , { customAttributes : { foo : 'bar' } } )
1096+ expect ( measurements . customAttributes ) . toEqual ( { foo : 'bar' }
1097+ )
1098+ } )
1099+ } )
9661100 } )
9671101 } )
9681102
0 commit comments