@@ -10,6 +10,7 @@ import { withSentry } from '../src/handler';
10
10
11
11
const MOCK_ENV = {
12
12
SENTRY_DSN :
'https://[email protected] /1337' ,
13
+ SENTRY_RELEASE : '1.1.1' ,
13
14
} ;
14
15
15
16
describe ( 'withSentry' , ( ) => {
@@ -51,6 +52,65 @@ describe('withSentry', () => {
51
52
52
53
expect ( result ) . toBe ( response ) ;
53
54
} ) ;
55
+
56
+ test ( 'merges options from env and callback' , async ( ) => {
57
+ const handler = {
58
+ fetch ( _request , _env , _context ) {
59
+ throw new Error ( 'test' ) ;
60
+ } ,
61
+ } satisfies ExportedHandler < typeof MOCK_ENV > ;
62
+
63
+ let sentryEvent : Event = { } ;
64
+
65
+ const wrappedHandler = withSentry (
66
+ env => ( {
67
+ dsn : env . SENTRY_DSN ,
68
+ beforeSend ( event ) {
69
+ sentryEvent = event ;
70
+ return null ;
71
+ } ,
72
+ } ) ,
73
+ handler ,
74
+ ) ;
75
+
76
+ try {
77
+ await wrappedHandler . fetch ( new Request ( 'https://example.com' ) , MOCK_ENV , createMockExecutionContext ( ) ) ;
78
+ } catch {
79
+ // ignore
80
+ }
81
+
82
+ expect ( sentryEvent . release ) . toEqual ( '1.1.1' ) ;
83
+ } ) ;
84
+
85
+ test ( 'callback options take precedence over env options' , async ( ) => {
86
+ const handler = {
87
+ fetch ( _request , _env , _context ) {
88
+ throw new Error ( 'test' ) ;
89
+ } ,
90
+ } satisfies ExportedHandler < typeof MOCK_ENV > ;
91
+
92
+ let sentryEvent : Event = { } ;
93
+
94
+ const wrappedHandler = withSentry (
95
+ env => ( {
96
+ dsn : env . SENTRY_DSN ,
97
+ release : '2.0.0' ,
98
+ beforeSend ( event ) {
99
+ sentryEvent = event ;
100
+ return null ;
101
+ } ,
102
+ } ) ,
103
+ handler ,
104
+ ) ;
105
+
106
+ try {
107
+ await wrappedHandler . fetch ( new Request ( 'https://example.com' ) , MOCK_ENV , createMockExecutionContext ( ) ) ;
108
+ } catch {
109
+ // ignore
110
+ }
111
+
112
+ expect ( sentryEvent . release ) . toEqual ( '2.0.0' ) ;
113
+ } ) ;
54
114
} ) ;
55
115
56
116
describe ( 'scheduled handler' , ( ) => {
@@ -70,6 +130,55 @@ describe('withSentry', () => {
70
130
expect ( optionsCallback ) . toHaveBeenLastCalledWith ( MOCK_ENV ) ;
71
131
} ) ;
72
132
133
+ test ( 'merges options from env and callback' , async ( ) => {
134
+ const handler = {
135
+ scheduled ( _controller , _env , _context ) {
136
+ SentryCore . captureMessage ( 'cloud_resource' ) ;
137
+ return ;
138
+ } ,
139
+ } satisfies ExportedHandler < typeof MOCK_ENV > ;
140
+
141
+ let sentryEvent : Event = { } ;
142
+ const wrappedHandler = withSentry (
143
+ env => ( {
144
+ dsn : env . SENTRY_DSN ,
145
+ beforeSend ( event ) {
146
+ sentryEvent = event ;
147
+ return null ;
148
+ } ,
149
+ } ) ,
150
+ handler ,
151
+ ) ;
152
+ await wrappedHandler . scheduled ( createMockScheduledController ( ) , MOCK_ENV , createMockExecutionContext ( ) ) ;
153
+
154
+ expect ( sentryEvent . release ) . toBe ( '1.1.1' ) ;
155
+ } ) ;
156
+
157
+ test ( 'callback options take precedence over env options' , async ( ) => {
158
+ const handler = {
159
+ scheduled ( _controller , _env , _context ) {
160
+ SentryCore . captureMessage ( 'cloud_resource' ) ;
161
+ return ;
162
+ } ,
163
+ } satisfies ExportedHandler < typeof MOCK_ENV > ;
164
+
165
+ let sentryEvent : Event = { } ;
166
+ const wrappedHandler = withSentry (
167
+ env => ( {
168
+ dsn : env . SENTRY_DSN ,
169
+ release : '2.0.0' ,
170
+ beforeSend ( event ) {
171
+ sentryEvent = event ;
172
+ return null ;
173
+ } ,
174
+ } ) ,
175
+ handler ,
176
+ ) ;
177
+ await wrappedHandler . scheduled ( createMockScheduledController ( ) , MOCK_ENV , createMockExecutionContext ( ) ) ;
178
+
179
+ expect ( sentryEvent . release ) . toEqual ( '2.0.0' ) ;
180
+ } ) ;
181
+
73
182
test ( 'flushes the event after the handler is done using the cloudflare context.waitUntil' , async ( ) => {
74
183
const handler = {
75
184
scheduled ( _controller , _env , _context ) {
0 commit comments