1
- import type { EventProcessor , Integration } from '@sentry/types' ;
1
+ import type { Client , Event , EventHint , Hub , Integration } from '@sentry/types' ;
2
2
3
3
import { Debug } from '../src/debug' ;
4
4
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
+ }
8
31
9
32
// Replace console log with a mock so we can check for invocations
10
33
const mockConsoleLog = jest . fn ( ) ;
@@ -24,56 +47,46 @@ describe('Debug integration setup should register an event processor that', () =
24
47
25
48
it ( 'logs an event' , ( ) => {
26
49
const debugIntegration = new Debug ( ) ;
50
+ const testEvent = { event_id : 'some event' } ;
27
51
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 ) ;
34
53
35
- debugIntegration . setupOnce ( captureEventProcessor , ( ) => mockGetCurrentHub ( debugIntegration ) as any ) ;
54
+ expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 1 ) ;
55
+ expect ( mockConsoleLog ) . toBeCalledWith ( testEvent ) ;
36
56
} ) ;
37
57
38
58
it ( 'logs an event hint if available' , ( ) => {
39
59
const debugIntegration = new Debug ( ) ;
40
60
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' } ;
49
63
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 ) ;
51
69
} ) ;
52
70
53
71
it ( 'logs events in stringified format when `stringify` option was set' , ( ) => {
54
72
const debugIntegration = new Debug ( { stringify : true } ) ;
73
+ const testEvent = { event_id : 'some event' } ;
55
74
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 ) ;
62
76
63
- debugIntegration . setupOnce ( captureEventProcessor , ( ) => mockGetCurrentHub ( debugIntegration ) as any ) ;
77
+ expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 1 ) ;
78
+ expect ( mockConsoleLog ) . toBeCalledWith ( JSON . stringify ( testEvent , null , 2 ) ) ;
64
79
} ) ;
65
80
66
81
it ( 'logs event hints in stringified format when `stringify` option was set' , ( ) => {
67
82
const debugIntegration = new Debug ( { stringify : true } ) ;
68
83
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 ) ;
76
88
77
- debugIntegration . setupOnce ( captureEventProcessor , ( ) => mockGetCurrentHub ( debugIntegration ) as any ) ;
89
+ expect ( mockConsoleLog ) . toHaveBeenCalledTimes ( 2 ) ;
90
+ expect ( mockConsoleLog ) . toBeCalledWith ( JSON . stringify ( testEventHint , null , 2 ) ) ;
78
91
} ) ;
79
92
} ) ;
0 commit comments