/
router.test.ts
216 lines (179 loc) · 6.73 KB
/
router.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import * as SentryBrowser from '@sentry/browser';
import { Transaction } from '@sentry/types';
import { vueRouterInstrumentation } from '../src';
import { Route } from '../src/router';
import * as vueTracing from '../src/tracing';
const captureExceptionSpy = jest.spyOn(SentryBrowser, 'captureException');
const mockVueRouter = {
onError: jest.fn<void, [(error: Error) => void]>(),
beforeEach: jest.fn<void, [(from: Route, to: Route, next: () => void) => void]>(),
};
const mockStartTransaction = jest.fn();
const mockNext = jest.fn();
const testRoutes: Record<string, Route> = {
initialPageloadRoute: { matched: [], params: {}, path: '', query: {} },
normalRoute1: {
matched: [{ path: '/books/:bookId/chapter/:chapterId' }],
params: {
bookId: '12',
chapterId: '3',
},
path: '/books/12/chapter/3',
query: {
utm_source: 'google',
},
},
normalRoute2: {
matched: [{ path: '/accounts/:accountId' }],
params: {
accountId: '4',
},
path: '/accounts/4',
query: {},
},
namedRoute: {
matched: [{ path: '/login' }],
name: 'login-screen',
params: {},
path: '/login',
query: {},
},
unmatchedRoute: {
matched: [],
params: {},
path: '/e8733846-20ac-488c-9871-a5cbcb647294',
query: {},
},
};
describe('vueRouterInstrumentation()', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should return instrumentation that instruments VueRouter.onError', () => {
// create instrumentation
const instrument = vueRouterInstrumentation(mockVueRouter);
// instrument
instrument(mockStartTransaction);
// check
expect(mockVueRouter.onError).toHaveBeenCalledTimes(1);
const onErrorCallback = mockVueRouter.onError.mock.calls[0][0];
const testError = new Error();
onErrorCallback(testError);
expect(captureExceptionSpy).toHaveBeenCalledTimes(1);
expect(captureExceptionSpy).toHaveBeenCalledWith(testError);
});
it.each([
['normalRoute1', 'normalRoute2', '/accounts/:accountId', 'route'],
['normalRoute2', 'namedRoute', 'login-screen', 'custom'],
['normalRoute2', 'unmatchedRoute', '/e8733846-20ac-488c-9871-a5cbcb647294', 'url'],
])(
'should return instrumentation that instruments VueRouter.beforeEach(%s, %s) for navigations',
(fromKey, toKey, transactionName, transactionSource) => {
// create instrumentation
const instrument = vueRouterInstrumentation(mockVueRouter);
// instrument
instrument(mockStartTransaction, true, true);
// check
expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1);
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0];
const from = testRoutes[fromKey];
const to = testRoutes[toKey];
beforeEachCallback(to, from, mockNext);
// first startTx call happens when the instrumentation is initialized (for pageloads)
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
expect(mockStartTransaction).toHaveBeenCalledWith({
name: transactionName,
metadata: {
source: transactionSource,
},
data: {
params: to.params,
query: to.query,
},
op: 'navigation',
tags: {
'routing.instrumentation': 'vue-router',
},
});
expect(mockNext).toHaveBeenCalledTimes(1);
},
);
it.each([
['initialPageloadRoute', 'normalRoute1', '/books/:bookId/chapter/:chapterId', 'route'],
['initialPageloadRoute', 'namedRoute', 'login-screen', 'custom'],
['initialPageloadRoute', 'unmatchedRoute', '/e8733846-20ac-488c-9871-a5cbcb647294', 'url'],
])(
'should return instrumentation that instruments VueRouter.beforeEach(%s, %s) for pageloads',
(fromKey, toKey, _transactionName, _transactionSource) => {
const mockedTxn = {
setName: jest.fn(),
setData: jest.fn(),
};
const customMockStartTxn = { ...mockStartTransaction }.mockImplementation(_ => {
return mockedTxn;
});
jest.spyOn(vueTracing, 'getActiveTransaction').mockImplementation(() => mockedTxn as unknown as Transaction);
// create instrumentation
const instrument = vueRouterInstrumentation(mockVueRouter);
// instrument
instrument(customMockStartTxn, true, true);
// check for transaction start
expect(customMockStartTxn).toHaveBeenCalledTimes(1);
expect(customMockStartTxn).toHaveBeenCalledWith({
name: '/',
metadata: {
source: 'url',
},
op: 'pageload',
tags: {
'routing.instrumentation': 'vue-router',
},
});
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0];
const from = testRoutes[fromKey];
const to = testRoutes[toKey];
beforeEachCallback(to, from, mockNext);
expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1);
expect(mockedTxn.setName).toHaveBeenCalledWith(_transactionName, _transactionSource);
expect(mockedTxn.setData).toHaveBeenNthCalledWith(1, 'params', to.params);
expect(mockedTxn.setData).toHaveBeenNthCalledWith(2, 'query', to.query);
expect(mockNext).toHaveBeenCalledTimes(1);
},
);
test.each([
[undefined, 1],
[false, 0],
[true, 1],
])(
'should return instrumentation that considers the startTransactionOnPageLoad option = %p',
(startTransactionOnPageLoad, expectedCallsAmount) => {
// create instrumentation
const instrument = vueRouterInstrumentation(mockVueRouter);
// instrument
instrument(mockStartTransaction, startTransactionOnPageLoad, true);
// check
expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1);
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0];
beforeEachCallback(testRoutes['normalRoute1'], testRoutes['initialPageloadRoute'], mockNext);
expect(mockStartTransaction).toHaveBeenCalledTimes(expectedCallsAmount);
},
);
test.each([
[undefined, 1],
[false, 0],
[true, 1],
])(
'should return instrumentation that considers the startTransactionOnLocationChange option = %p',
(startTransactionOnLocationChange, expectedCallsAmount) => {
// create instrumentation
const instrument = vueRouterInstrumentation(mockVueRouter);
// instrument (this will call startTrransaction once for pageloads but we can ignore that)
instrument(mockStartTransaction, true, startTransactionOnLocationChange);
// check
expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1);
const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0];
beforeEachCallback(testRoutes['normalRoute2'], testRoutes['normalRoute1'], mockNext);
expect(mockStartTransaction).toHaveBeenCalledTimes(expectedCallsAmount + 1);
},
);
});