1- import type { EventProcessor , Integration } from '@sentry/types' ;
1+ import type { Client , Event , EventHint , Hub , Integration } from '@sentry/types' ;
22
33import { Debug } from '../src/debug' ;
44
5- const mockGetCurrentHub = ( getIntegrationResult : Integration ) => ( {
6- getIntegration : jest . fn ( ( ) => getIntegrationResult ) ,
7- } ) ;
5+ function testEventLogged ( integration : Integration , testEvent ?: Event , testEventHint ?: EventHint ) {
6+ const callbacks : ( ( event : Event , hint ?: EventHint ) => void ) [ ] = [ ] ;
7+
8+ const client : Client = {
9+ on ( hook : string , callback : ( event : Event , hint ?: EventHint ) => void ) {
10+ expect ( hook ) . toEqual ( 'beforeSendEvent' ) ;
11+ callbacks . push ( callback ) ;
12+ } ,
13+ } as Client ;
14+
15+ function getCurrentHub ( ) {
16+ return {
17+ getClient : jest . fn ( ( ) => {
18+ return client ;
19+ } ) ,
20+ } as unknown as Hub ;
21+ }
22+
23+ integration . setupOnce ( ( ) => { } , getCurrentHub ) ;
24+
25+ expect ( callbacks . length ) . toEqual ( 1 ) ;
26+
27+ if ( testEvent ) {
28+ callbacks [ 0 ] ( testEvent , testEventHint ) ;
29+ }
30+ }
831
932// Replace console log with a mock so we can check for invocations
1033const mockConsoleLog = jest . fn ( ) ;
@@ -24,56 +47,46 @@ describe('Debug integration setup should register an event processor that', () =
2447
2548 it ( 'logs an event' , ( ) => {
2649 const debugIntegration = new Debug ( ) ;
50+ const testEvent = { event_id : 'some event' } ;
2751
28- const captureEventProcessor = ( eventProcessor : EventProcessor ) => {
29- const testEvent = { event_id : 'some event' } ;
30- void eventProcessor ( testEvent , { } ) ;
31- expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 1 ) ;
32- expect ( mockConsoleLog ) . toBeCalledWith ( testEvent ) ;
33- } ;
52+ testEventLogged ( debugIntegration , testEvent ) ;
3453
35- debugIntegration . setupOnce ( captureEventProcessor , ( ) => mockGetCurrentHub ( debugIntegration ) as any ) ;
54+ expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 1 ) ;
55+ expect ( mockConsoleLog ) . toBeCalledWith ( testEvent ) ;
3656 } ) ;
3757
3858 it ( 'logs an event hint if available' , ( ) => {
3959 const debugIntegration = new Debug ( ) ;
4060
41- const captureEventProcessor = ( eventProcessor : EventProcessor ) => {
42- const testEvent = { event_id : 'some event' } ;
43- const testEventHint = { event_id : 'some event hint' } ;
44- void eventProcessor ( testEvent , testEventHint ) ;
45- expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 2 ) ;
46- expect ( mockConsoleLog ) . toBeCalledWith ( testEvent ) ;
47- expect ( mockConsoleLog ) . toBeCalledWith ( testEventHint ) ;
48- } ;
61+ const testEvent = { event_id : 'some event' } ;
62+ const testEventHint = { event_id : 'some event hint' } ;
4963
50- debugIntegration . setupOnce ( captureEventProcessor , ( ) => mockGetCurrentHub ( debugIntegration ) as any ) ;
64+ testEventLogged ( debugIntegration , testEvent , testEventHint ) ;
65+
66+ expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 2 ) ;
67+ expect ( mockConsoleLog ) . toBeCalledWith ( testEvent ) ;
68+ expect ( mockConsoleLog ) . toBeCalledWith ( testEventHint ) ;
5169 } ) ;
5270
5371 it ( 'logs events in stringified format when `stringify` option was set' , ( ) => {
5472 const debugIntegration = new Debug ( { stringify : true } ) ;
73+ const testEvent = { event_id : 'some event' } ;
5574
56- const captureEventProcessor = ( eventProcessor : EventProcessor ) => {
57- const testEvent = { event_id : 'some event' } ;
58- void eventProcessor ( testEvent , { } ) ;
59- expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 1 ) ;
60- expect ( mockConsoleLog ) . toBeCalledWith ( JSON . stringify ( testEvent , null , 2 ) ) ;
61- } ;
75+ testEventLogged ( debugIntegration , testEvent ) ;
6276
63- debugIntegration . setupOnce ( captureEventProcessor , ( ) => mockGetCurrentHub ( debugIntegration ) as any ) ;
77+ expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 1 ) ;
78+ expect ( mockConsoleLog ) . toBeCalledWith ( JSON . stringify ( testEvent , null , 2 ) ) ;
6479 } ) ;
6580
6681 it ( 'logs event hints in stringified format when `stringify` option was set' , ( ) => {
6782 const debugIntegration = new Debug ( { stringify : true } ) ;
6883
69- const captureEventProcessor = ( eventProcessor : EventProcessor ) => {
70- const testEvent = { event_id : 'some event' } ;
71- const testEventHint = { event_id : 'some event hint' } ;
72- void eventProcessor ( testEvent , testEventHint ) ;
73- expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 2 ) ;
74- expect ( mockConsoleLog ) . toBeCalledWith ( JSON . stringify ( testEventHint , null , 2 ) ) ;
75- } ;
84+ const testEvent = { event_id : 'some event' } ;
85+ const testEventHint = { event_id : 'some event hint' } ;
86+
87+ testEventLogged ( debugIntegration , testEvent , testEventHint ) ;
7688
77- debugIntegration . setupOnce ( captureEventProcessor , ( ) => mockGetCurrentHub ( debugIntegration ) as any ) ;
89+ expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 2 ) ;
90+ expect ( mockConsoleLog ) . toBeCalledWith ( JSON . stringify ( testEventHint , null , 2 ) ) ;
7891 } ) ;
7992} ) ;
0 commit comments