@@ -11,6 +11,8 @@ import { AMPLITUDE_WRITEKEY } from '../../../test-helpers/test-writekeys'
1111import { PersistedPriorityQueue } from '../../../lib/priority-queue/persisted'
1212import * as Factory from '../../../test-helpers/factories'
1313import { cdnSettingsMinimal } from '../../../test-helpers/fixtures'
14+ import * as MetricHelpers from '../../../core/stats/metric-helpers'
15+ import * as ScriptLoader from '../../../lib/load-script'
1416
1517const cdnResponse : CDNSettings = {
1618 ...cdnSettingsMinimal ,
@@ -838,3 +840,65 @@ describe('option overrides', () => {
838840 )
839841 } )
840842} )
843+
844+ describe ( 'load error metrics' , ( ) => {
845+ let metricSpy : jest . SpyInstance
846+
847+ beforeEach ( ( ) => {
848+ jest . restoreAllMocks ( )
849+ jest . resetAllMocks ( )
850+ metricSpy = jest . spyOn ( MetricHelpers , 'recordIntegrationMetric' )
851+ } )
852+
853+ it ( 'records integration invoke error metric when loadIntegration fails' , async ( ) => {
854+ jest
855+ . spyOn ( ScriptLoader , 'loadScript' )
856+ . mockRejectedValue ( new Error ( 'Script load failed' ) )
857+
858+ const dest = new LegacyDestination (
859+ 'Broken Integration' ,
860+ 'latest' ,
861+ 'writeKey' ,
862+ { } ,
863+ { }
864+ )
865+
866+ const ctx = Context . system ( )
867+
868+ await expect ( dest . load ( ctx , { } as Analytics ) ) . rejects . toThrow (
869+ 'Script load failed'
870+ )
871+
872+ expect ( metricSpy ) . toHaveBeenCalledWith ( ctx , {
873+ integrationName : 'Broken Integration' ,
874+ methodName : 'load' ,
875+ type : 'classic' ,
876+ didError : true ,
877+ } )
878+ } )
879+
880+ it ( 'records integration invoke error metric when buildIntegration fails' , async ( ) => {
881+ // Provide an integrationSource that will cause buildIntegration to throw
882+ const badSource = { } as any
883+
884+ const dest = new LegacyDestination (
885+ 'Bad Build Integration' ,
886+ 'latest' ,
887+ 'writeKey' ,
888+ { } ,
889+ { } ,
890+ badSource
891+ )
892+
893+ const ctx = Context . system ( )
894+
895+ await expect ( dest . load ( ctx , { } as Analytics ) ) . rejects . toThrow ( )
896+
897+ expect ( metricSpy ) . toHaveBeenCalledWith ( ctx , {
898+ integrationName : 'Bad Build Integration' ,
899+ methodName : 'load' ,
900+ type : 'classic' ,
901+ didError : true ,
902+ } )
903+ } )
904+ } )
0 commit comments