1
1
import * as SentryBrowser from '@sentry/browser' ;
2
+ import * as SentryCore from '@sentry/core' ;
3
+ import type { Span } from '@sentry/core' ;
2
4
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN , SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core' ;
3
- import type { SpanAttributes , Transaction } from '@sentry/types' ;
5
+ import type { SpanAttributes } from '@sentry/types' ;
4
6
5
7
import type { Route } from '../src/router' ;
6
8
import { instrumentVueRouter } from '../src/router' ;
7
- import * as vueTracing from '../src/tracing' ;
8
9
9
10
const captureExceptionSpy = jest . spyOn ( SentryBrowser , 'captureException' ) ;
11
+ jest . mock ( '@sentry/core' , ( ) => {
12
+ const actual = jest . requireActual ( '@sentry/core' ) ;
13
+ return {
14
+ ...actual ,
15
+ getActiveSpan : jest . fn ( ) . mockReturnValue ( { } ) ,
16
+ } ;
17
+ } ) ;
10
18
11
19
const mockVueRouter = {
12
20
onError : jest . fn < void , [ ( error : Error ) => void ] > ( ) ,
@@ -120,18 +128,16 @@ describe('instrumentVueRouter()', () => {
120
128
] ) (
121
129
'should return instrumentation that instruments VueRouter.beforeEach(%s, %s) for pageloads' ,
122
130
( fromKey , toKey , transactionName , transactionSource ) => {
123
- const mockedTxn = {
131
+ const mockRootSpan = {
124
132
updateName : jest . fn ( ) ,
125
- setData : jest . fn ( ) ,
126
133
setAttribute : jest . fn ( ) ,
127
134
setAttributes : jest . fn ( ) ,
128
- metadata : { } ,
129
135
} ;
130
136
131
- jest . spyOn ( vueTracing , 'getActiveTransaction ' ) . mockImplementation ( ( ) => mockedTxn as unknown as Transaction ) ;
137
+ jest . spyOn ( SentryCore , 'getRootSpan ' ) . mockImplementation ( ( ) => mockRootSpan as unknown as Span ) ;
132
138
133
139
const mockStartSpan = jest . fn ( ) . mockImplementation ( _ => {
134
- return mockedTxn ;
140
+ return mockRootSpan ;
135
141
} ) ;
136
142
instrumentVueRouter (
137
143
mockVueRouter ,
@@ -150,9 +156,9 @@ describe('instrumentVueRouter()', () => {
150
156
beforeEachCallback ( to , from , mockNext ) ;
151
157
expect ( mockVueRouter . beforeEach ) . toHaveBeenCalledTimes ( 1 ) ;
152
158
153
- expect ( mockedTxn . updateName ) . toHaveBeenCalledWith ( transactionName ) ;
154
- expect ( mockedTxn . setAttribute ) . toHaveBeenCalledWith ( SEMANTIC_ATTRIBUTE_SENTRY_SOURCE , transactionSource ) ;
155
- expect ( mockedTxn . setAttributes ) . toHaveBeenCalledWith ( {
159
+ expect ( mockRootSpan . updateName ) . toHaveBeenCalledWith ( transactionName ) ;
160
+ expect ( mockRootSpan . setAttribute ) . toHaveBeenCalledWith ( SEMANTIC_ATTRIBUTE_SENTRY_SOURCE , transactionSource ) ;
161
+ expect ( mockRootSpan . setAttributes ) . toHaveBeenCalledWith ( {
156
162
[ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.pageload.vue' ,
157
163
...getAttributesForRoute ( to ) ,
158
164
} ) ;
@@ -216,9 +222,8 @@ describe('instrumentVueRouter()', () => {
216
222
} ) ;
217
223
218
224
it ( "doesn't overwrite a pageload transaction name it was set to custom before the router resolved the route" , ( ) => {
219
- const mockedTxn = {
225
+ const mockRootSpan = {
220
226
updateName : jest . fn ( ) ,
221
- setData : jest . fn ( ) ,
222
227
setAttribute : jest . fn ( ) ,
223
228
setAttributes : jest . fn ( ) ,
224
229
name : '' ,
@@ -229,9 +234,9 @@ describe('instrumentVueRouter()', () => {
229
234
} ) ,
230
235
} ;
231
236
const mockStartSpan = jest . fn ( ) . mockImplementation ( _ => {
232
- return mockedTxn ;
237
+ return mockRootSpan ;
233
238
} ) ;
234
- jest . spyOn ( vueTracing , 'getActiveTransaction ' ) . mockImplementation ( ( ) => mockedTxn as unknown as Transaction ) ;
239
+ jest . spyOn ( SentryCore , 'getRootSpan ' ) . mockImplementation ( ( ) => mockRootSpan as unknown as Span ) ;
235
240
236
241
instrumentVueRouter (
237
242
mockVueRouter ,
@@ -244,8 +249,8 @@ describe('instrumentVueRouter()', () => {
244
249
245
250
// now we give the transaction a custom name, thereby simulating what would
246
251
// happen when users use the `beforeNavigate` hook
247
- mockedTxn . name = 'customTxnName' ;
248
- mockedTxn . toJSON = ( ) => ( {
252
+ mockRootSpan . name = 'customTxnName' ;
253
+ mockRootSpan . toJSON = ( ) => ( {
249
254
data : {
250
255
[ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'custom' ,
251
256
} ,
@@ -260,13 +265,13 @@ describe('instrumentVueRouter()', () => {
260
265
261
266
expect ( mockVueRouter . beforeEach ) . toHaveBeenCalledTimes ( 1 ) ;
262
267
263
- expect ( mockedTxn . updateName ) . not . toHaveBeenCalled ( ) ;
264
- expect ( mockedTxn . setAttribute ) . not . toHaveBeenCalled ( ) ;
265
- expect ( mockedTxn . setAttributes ) . toHaveBeenCalledWith ( {
268
+ expect ( mockRootSpan . updateName ) . not . toHaveBeenCalled ( ) ;
269
+ expect ( mockRootSpan . setAttribute ) . not . toHaveBeenCalled ( ) ;
270
+ expect ( mockRootSpan . setAttributes ) . toHaveBeenCalledWith ( {
266
271
[ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.pageload.vue' ,
267
272
...getAttributesForRoute ( to ) ,
268
273
} ) ;
269
- expect ( mockedTxn . name ) . toEqual ( 'customTxnName' ) ;
274
+ expect ( mockRootSpan . name ) . toEqual ( 'customTxnName' ) ;
270
275
} ) ;
271
276
272
277
test . each ( [
@@ -275,7 +280,7 @@ describe('instrumentVueRouter()', () => {
275
280
] ) (
276
281
'should return instrumentation that considers the instrumentPageLoad = %p' ,
277
282
( instrumentPageLoad , expectedCallsAmount ) => {
278
- const mockedTxn = {
283
+ const mockRootSpan = {
279
284
updateName : jest . fn ( ) ,
280
285
setData : jest . fn ( ) ,
281
286
setAttribute : jest . fn ( ) ,
@@ -287,7 +292,7 @@ describe('instrumentVueRouter()', () => {
287
292
} ,
288
293
} ) ,
289
294
} ;
290
- jest . spyOn ( vueTracing , 'getActiveTransaction ' ) . mockImplementation ( ( ) => mockedTxn as unknown as Transaction ) ;
295
+ jest . spyOn ( SentryCore , 'getRootSpan ' ) . mockImplementation ( ( ) => mockRootSpan as unknown as Span ) ;
291
296
292
297
const mockStartSpan = jest . fn ( ) ;
293
298
instrumentVueRouter (
@@ -302,7 +307,7 @@ describe('instrumentVueRouter()', () => {
302
307
const beforeEachCallback = mockVueRouter . beforeEach . mock . calls [ 0 ] [ 0 ] ;
303
308
beforeEachCallback ( testRoutes [ 'normalRoute1' ] , testRoutes [ 'initialPageloadRoute' ] , mockNext ) ;
304
309
305
- expect ( mockedTxn . updateName ) . toHaveBeenCalledTimes ( expectedCallsAmount ) ;
310
+ expect ( mockRootSpan . updateName ) . toHaveBeenCalledTimes ( expectedCallsAmount ) ;
306
311
expect ( mockStartSpan ) . not . toHaveBeenCalled ( ) ;
307
312
} ,
308
313
) ;
0 commit comments