Skip to content

Commit 8734f03

Browse files
committed
fix tests
1 parent 2a16648 commit 8734f03

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

packages/vue/test/router.test.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import * as SentryBrowser from '@sentry/browser';
2+
import * as SentryCore from '@sentry/core';
3+
import type { Span } from '@sentry/core';
24
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';
46

57
import type { Route } from '../src/router';
68
import { instrumentVueRouter } from '../src/router';
7-
import * as vueTracing from '../src/tracing';
89

910
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+
});
1018

1119
const mockVueRouter = {
1220
onError: jest.fn<void, [(error: Error) => void]>(),
@@ -120,18 +128,16 @@ describe('instrumentVueRouter()', () => {
120128
])(
121129
'should return instrumentation that instruments VueRouter.beforeEach(%s, %s) for pageloads',
122130
(fromKey, toKey, transactionName, transactionSource) => {
123-
const mockedTxn = {
131+
const mockRootSpan = {
124132
updateName: jest.fn(),
125-
setData: jest.fn(),
126133
setAttribute: jest.fn(),
127134
setAttributes: jest.fn(),
128-
metadata: {},
129135
};
130136

131-
jest.spyOn(vueTracing, 'getActiveTransaction').mockImplementation(() => mockedTxn as unknown as Transaction);
137+
jest.spyOn(SentryCore, 'getRootSpan').mockImplementation(() => mockRootSpan as unknown as Span);
132138

133139
const mockStartSpan = jest.fn().mockImplementation(_ => {
134-
return mockedTxn;
140+
return mockRootSpan;
135141
});
136142
instrumentVueRouter(
137143
mockVueRouter,
@@ -150,9 +156,9 @@ describe('instrumentVueRouter()', () => {
150156
beforeEachCallback(to, from, mockNext);
151157
expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1);
152158

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({
156162
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.vue',
157163
...getAttributesForRoute(to),
158164
});
@@ -216,9 +222,8 @@ describe('instrumentVueRouter()', () => {
216222
});
217223

218224
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 = {
220226
updateName: jest.fn(),
221-
setData: jest.fn(),
222227
setAttribute: jest.fn(),
223228
setAttributes: jest.fn(),
224229
name: '',
@@ -229,9 +234,9 @@ describe('instrumentVueRouter()', () => {
229234
}),
230235
};
231236
const mockStartSpan = jest.fn().mockImplementation(_ => {
232-
return mockedTxn;
237+
return mockRootSpan;
233238
});
234-
jest.spyOn(vueTracing, 'getActiveTransaction').mockImplementation(() => mockedTxn as unknown as Transaction);
239+
jest.spyOn(SentryCore, 'getRootSpan').mockImplementation(() => mockRootSpan as unknown as Span);
235240

236241
instrumentVueRouter(
237242
mockVueRouter,
@@ -244,8 +249,8 @@ describe('instrumentVueRouter()', () => {
244249

245250
// now we give the transaction a custom name, thereby simulating what would
246251
// happen when users use the `beforeNavigate` hook
247-
mockedTxn.name = 'customTxnName';
248-
mockedTxn.toJSON = () => ({
252+
mockRootSpan.name = 'customTxnName';
253+
mockRootSpan.toJSON = () => ({
249254
data: {
250255
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom',
251256
},
@@ -260,13 +265,13 @@ describe('instrumentVueRouter()', () => {
260265

261266
expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1);
262267

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({
266271
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.vue',
267272
...getAttributesForRoute(to),
268273
});
269-
expect(mockedTxn.name).toEqual('customTxnName');
274+
expect(mockRootSpan.name).toEqual('customTxnName');
270275
});
271276

272277
test.each([
@@ -275,7 +280,7 @@ describe('instrumentVueRouter()', () => {
275280
])(
276281
'should return instrumentation that considers the instrumentPageLoad = %p',
277282
(instrumentPageLoad, expectedCallsAmount) => {
278-
const mockedTxn = {
283+
const mockRootSpan = {
279284
updateName: jest.fn(),
280285
setData: jest.fn(),
281286
setAttribute: jest.fn(),
@@ -287,7 +292,7 @@ describe('instrumentVueRouter()', () => {
287292
},
288293
}),
289294
};
290-
jest.spyOn(vueTracing, 'getActiveTransaction').mockImplementation(() => mockedTxn as unknown as Transaction);
295+
jest.spyOn(SentryCore, 'getRootSpan').mockImplementation(() => mockRootSpan as unknown as Span);
291296

292297
const mockStartSpan = jest.fn();
293298
instrumentVueRouter(
@@ -302,7 +307,7 @@ describe('instrumentVueRouter()', () => {
302307
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0];
303308
beforeEachCallback(testRoutes['normalRoute1'], testRoutes['initialPageloadRoute'], mockNext);
304309

305-
expect(mockedTxn.updateName).toHaveBeenCalledTimes(expectedCallsAmount);
310+
expect(mockRootSpan.updateName).toHaveBeenCalledTimes(expectedCallsAmount);
306311
expect(mockStartSpan).not.toHaveBeenCalled();
307312
},
308313
);

0 commit comments

Comments
 (0)