@@ -66,37 +66,18 @@ describe('webWorkerIntegration', () => {
6666 vi . clearAllMocks ( ) ;
6767 } ) ;
6868
69- describe ( 'integration creation' , ( ) => {
70- it ( 'creates integration with correct name' , ( ) => {
71- const integration = webWorkerIntegration ( { worker : mockWorker as any } ) ;
72-
73- expect ( integration . name ) . toBe ( INTEGRATION_NAME ) ;
74- expect ( integration . name ) . toBe ( 'WebWorker' ) ;
75- } ) ;
76-
77- it ( 'returns a properly structured integration object' , ( ) => {
78- const integration = webWorkerIntegration ( { worker : mockWorker as any } ) ;
79-
80- expect ( typeof integration ) . toBe ( 'object' ) ;
81- expect ( integration . name ) . toBeDefined ( ) ;
82- expect ( integration . setupOnce ) . toBeDefined ( ) ;
83- } ) ;
84-
85- it ( 'returns integration object with setupOnce function' , ( ) => {
86- const integration = webWorkerIntegration ( { worker : mockWorker as any } ) ;
69+ it ( 'creates integration with correct name' , ( ) => {
70+ const integration = webWorkerIntegration ( { worker : mockWorker as any } ) ;
8771
88- expect ( integration ) . toMatchObject ( {
89- name : 'WebWorker' ,
90- setupOnce : expect . any ( Function ) ,
91- } ) ;
92- } ) ;
72+ expect ( integration . name ) . toBe ( INTEGRATION_NAME ) ;
73+ expect ( integration . name ) . toBe ( 'WebWorker' ) ;
74+ expect ( typeof integration . setupOnce ) . toBe ( 'function' ) ;
9375 } ) ;
9476
9577 describe ( 'setupOnce' , ( ) => {
9678 it ( 'adds message event listener to worker' , ( ) => {
9779 const integration = webWorkerIntegration ( { worker : mockWorker as any } ) ;
9880
99- expect ( integration . setupOnce ) . toBeDefined ( ) ;
10081 integration . setupOnce ! ( ) ;
10182
10283 expect ( mockWorker . addEventListener ) . toHaveBeenCalledWith ( 'message' , expect . any ( Function ) ) ;
@@ -107,7 +88,6 @@ describe('webWorkerIntegration', () => {
10788
10889 beforeEach ( ( ) => {
10990 const integration = webWorkerIntegration ( { worker : mockWorker as any } ) ;
110- expect ( integration . setupOnce ) . toBeDefined ( ) ;
11191 integration . setupOnce ! ( ) ;
11292
11393 // Extract the message handler from the addEventListener call
@@ -136,23 +116,10 @@ describe('webWorkerIntegration', () => {
136116 expect ( mockDebugLog ) . not . toHaveBeenCalled ( ) ;
137117 } ) ;
138118
139- it ( 'ignores messages without _sentry object' , ( ) => {
140- mockEvent . data = {
141- _sentryMessage : true ,
142- _sentryDebugIds : { 'file1.js' : 'debug-id-1' } ,
143- } ;
144-
145- messageHandler ( mockEvent ) ;
146-
147- expect ( mockEvent . stopImmediatePropagation ) . not . toHaveBeenCalled ( ) ;
148- expect ( mockDebugLog ) . not . toHaveBeenCalled ( ) ;
149- } ) ;
150-
151119 it ( 'processes valid Sentry messages' , ( ) => {
152120 mockEvent . data = {
153121 _sentryMessage : true ,
154122 _sentryDebugIds : { 'file1.js' : 'debug-id-1' } ,
155- _sentry : { } ,
156123 } ;
157124
158125 messageHandler ( mockEvent ) ;
@@ -170,7 +137,6 @@ describe('webWorkerIntegration', () => {
170137 'worker-file1.js' : 'worker-debug-1' ,
171138 'worker-file2.js' : 'worker-debug-2' ,
172139 } ,
173- _sentry : { } ,
174140 } ;
175141
176142 messageHandler ( mockEvent ) ;
@@ -193,7 +159,6 @@ describe('webWorkerIntegration', () => {
193159 'shared-file.js' : 'worker-debug-id' , // Should be overridden
194160 'worker-only.js' : 'worker-debug-3' , // Should be kept
195161 } ,
196- _sentry : { } ,
197162 } ;
198163
199164 messageHandler ( mockEvent ) ;
@@ -211,7 +176,6 @@ describe('webWorkerIntegration', () => {
211176 mockEvent . data = {
212177 _sentryMessage : true ,
213178 _sentryDebugIds : { } ,
214- _sentry : { } ,
215179 } ;
216180
217181 messageHandler ( mockEvent ) ;
@@ -241,6 +205,7 @@ describe('registerWebWorker', () => {
241205 it ( 'posts message with _sentryMessage flag' , ( ) => {
242206 registerWebWorker ( mockWorkerSelf as any ) ;
243207
208+ expect ( mockWorkerSelf . postMessage ) . toHaveBeenCalledTimes ( 1 ) ;
244209 expect ( mockWorkerSelf . postMessage ) . toHaveBeenCalledWith ( {
245210 _sentryMessage : true ,
246211 _sentryDebugIds : undefined ,
@@ -255,6 +220,7 @@ describe('registerWebWorker', () => {
255220
256221 registerWebWorker ( mockWorkerSelf as any ) ;
257222
223+ expect ( mockWorkerSelf . postMessage ) . toHaveBeenCalledTimes ( 1 ) ;
258224 expect ( mockWorkerSelf . postMessage ) . toHaveBeenCalledWith ( {
259225 _sentryMessage : true ,
260226 _sentryDebugIds : {
@@ -269,64 +235,57 @@ describe('registerWebWorker', () => {
269235
270236 registerWebWorker ( mockWorkerSelf as any ) ;
271237
238+ expect ( mockWorkerSelf . postMessage ) . toHaveBeenCalledTimes ( 1 ) ;
272239 expect ( mockWorkerSelf . postMessage ) . toHaveBeenCalledWith ( {
273240 _sentryMessage : true ,
274241 _sentryDebugIds : undefined ,
275242 } ) ;
276243 } ) ;
244+ } ) ;
277245
278- it ( 'handles empty debug IDs object' , ( ) => {
279- mockWorkerSelf . _sentryDebugIds = { } ;
280-
281- registerWebWorker ( mockWorkerSelf as any ) ;
246+ describe ( 'registerWebWorker and webWorkerIntegration' , ( ) => {
247+ beforeEach ( ( ) => { } ) ;
282248
283- expect ( mockWorkerSelf . postMessage ) . toHaveBeenCalledWith ( {
284- _sentryMessage : true ,
285- _sentryDebugIds : { } ,
286- } ) ;
287- } ) ;
249+ it ( 'works together' , ( ) => {
250+ ( helpers . WINDOW as any ) . _sentryDebugIds = {
251+ 'main-file1.js' : 'main-debug-1' ,
252+ 'main-file2.js' : 'main-debug-2' ,
253+ 'shared-file.js' : 'main-debug-id' ,
254+ } ;
288255
289- it ( 'calls postMessage exactly once' , ( ) => {
290- registerWebWorker ( mockWorkerSelf as any ) ;
256+ let cb : ( ( arg0 : any ) => any ) | undefined = undefined ;
291257
292- expect ( mockWorkerSelf . postMessage ) . toHaveBeenCalledTimes ( 1 ) ;
293- } ) ;
294- } ) ;
295-
296- describe ( 'event propagation ' , ( ) => {
297- // Since isWebWorkerMessage is not exported, we test it indirectly through the integration
298- let messageHandler : ( event : any ) => void ;
299- let mockWorker : { addEventListener : ReturnType < typeof vi . fn > } ;
258+ // Setup mock worker
259+ const mockWorker = {
260+ _sentryDebugIds : {
261+ 'worker-file1.js' : 'worker-debug-1' ,
262+ 'worker-file2.js' : 'worker-debug-2' ,
263+ 'shared-file.js' : 'worker-debug-id' ,
264+ } ,
265+ addEventListener : vi . fn ( ( _ , l ) => ( cb = l ) ) ,
266+ postMessage : vi . fn ( message => {
267+ // @ts -expect-error - cb is defined
268+ cb ( { data : message , stopImmediatePropagation : vi . fn ( ) } ) ;
269+ } ) ,
270+ } ;
300271
301- beforeEach ( ( ) => {
302- mockWorker = { addEventListener : vi . fn ( ) } ;
303272 const integration = webWorkerIntegration ( { worker : mockWorker as any } ) ;
304273 integration . setupOnce ! ( ) ;
305- expect ( mockWorker . addEventListener ) . toHaveBeenCalled ( ) ;
306- expect ( mockWorker . addEventListener . mock . calls ) . toBeDefined ( ) ;
307- messageHandler = mockWorker . addEventListener . mock . calls ! [ 0 ] ! [ 1 ] ;
308- } ) ;
309274
310- it . each ( [
311- [ 'plain object but missing _sentryMessage: { _sentry: {} }' , { _sentry : { } } ] ,
312- [
313- 'plain object but _sentryMessage is false: { _sentryMessage: false, _sentry: {} }' ,
314- { _sentryMessage : false , _sentry : { } } ,
315- ] ,
316- [ 'plain object but missing _sentry: { _sentryMessage: true }' , { _sentryMessage : true } ] ,
317- [
318- 'plain object but _sentry is not an object: { _sentryMessage: true, _sentry: "not-object" }' ,
319- { _sentryMessage : true , _sentry : 'not-object' } ,
320- ] ,
321- ] ) ( "doesn't stop propagation for %s" , ( _desc , data ) => {
322- const mockEvent = { stopImmediatePropagation : vi . fn ( ) , data } ;
323- messageHandler ( mockEvent ) ;
324- expect ( mockEvent . stopImmediatePropagation ) . not . toHaveBeenCalled ( ) ;
325- } ) ;
275+ registerWebWorker ( mockWorker as any ) ;
276+
277+ expect ( mockWorker . addEventListener ) . toHaveBeenCalledWith ( 'message' , expect . any ( Function ) ) ;
278+ expect ( mockWorker . postMessage ) . toHaveBeenCalledWith ( {
279+ _sentryMessage : true ,
280+ _sentryDebugIds : mockWorker . _sentryDebugIds ,
281+ } ) ;
326282
327- it ( 'stops propagation for sentry message' , ( ) => {
328- const mockEvent = { stopImmediatePropagation : vi . fn ( ) , data : { _sentryMessage : true , _sentryDebugIds : { } } } ;
329- messageHandler ( mockEvent ) ;
330- expect ( mockEvent . stopImmediatePropagation ) . toHaveBeenCalledTimes ( 1 ) ;
283+ expect ( ( helpers . WINDOW as any ) . _sentryDebugIds ) . toEqual ( {
284+ 'main-file1.js' : 'main-debug-1' ,
285+ 'main-file2.js' : 'main-debug-2' ,
286+ 'shared-file.js' : 'main-debug-id' ,
287+ 'worker-file1.js' : 'worker-debug-1' ,
288+ 'worker-file2.js' : 'worker-debug-2' ,
289+ } ) ;
331290 } ) ;
332291} ) ;
0 commit comments