-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
tracing.test.ts
139 lines (116 loc) · 5.03 KB
/
tracing.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
import { Event, NavigationStart, ResolveEnd, Router } from '@angular/router';
import { Subject } from 'rxjs';
import { instrumentAngularRouting, TraceService } from '../src/index';
describe('Angular Tracing', () => {
const startTransaction = jest.fn();
describe('instrumentAngularRouting', () => {
it('should attach the transaction source on the pageload transaction', () => {
instrumentAngularRouting(startTransaction);
expect(startTransaction).toHaveBeenCalledWith({
name: '/',
op: 'pageload',
metadata: { source: 'url' },
});
});
});
describe('TraceService', () => {
let traceService: TraceService;
const routerEvents$: Subject<Event> = new Subject();
const mockedRouter: Partial<Router> = {
events: routerEvents$,
};
beforeEach(() => {
instrumentAngularRouting(startTransaction);
jest.resetAllMocks();
traceService = new TraceService(mockedRouter as Router);
});
afterEach(() => {
traceService.ngOnDestroy();
});
it('attaches the transaction source on a navigation change', () => {
routerEvents$.next(new NavigationStart(0, 'user/123/credentials'));
expect(startTransaction).toHaveBeenCalledTimes(1);
expect(startTransaction).toHaveBeenCalledWith({
name: 'user/123/credentials',
op: 'navigation',
metadata: { source: 'url' },
});
});
describe('URL parameterization', () => {
// TODO: These tests are real unit tests in the sense that they only test TraceService
// and we essentially just simulate a router navigation by firing the respective
// routing events and providing the raw URL + the resolved route parameters.
// In the future we should add more "wholesome" tests that let the Angular router
// do its thing (e.g. by calling router.navigate) and we check how our service
// reacts to it.
// Once we set up Jest for testing Angular, we can use TestBed to inject an actual
// router instance into TraceService and add more tests.
let transaction: any;
let customStartTransaction: any;
beforeEach(() => {
transaction = {
setName: jest.fn(name => (transaction.name = name)),
setMetadata: jest.fn(metadata => (transaction.metadata = metadata)),
};
customStartTransaction = jest.fn((ctx: any) => {
transaction.name = ctx.name;
transaction.op = ctx.op;
transaction.metadata = ctx.metadata;
return transaction;
});
});
it.each([
['does not alter static routes', '/books/', {}, '/books/'],
['parameterizes number IDs in the URL', '/books/1/details', { bookId: '1' }, '/books/:bookId/details'],
[
'parameterizes string IDs in the URL',
'/books/asd123/details',
{ bookId: 'asd123' },
'/books/:bookId/details',
],
[
'parameterizes UUID4 IDs in the URL',
'/books/04bc6846-4a1e-4af5-984a-003258f33e31/details',
{ bookId: '04bc6846-4a1e-4af5-984a-003258f33e31' },
'/books/:bookId/details',
],
[
'parameterizes multiple IDs in the URL',
'/org/sentry/projects/1234/events/04bc6846-4a1e-4af5-984a-003258f33e31',
{ orgId: 'sentry', projId: '1234', eventId: '04bc6846-4a1e-4af5-984a-003258f33e31' },
'/org/:orgId/projects/:projId/events/:eventId',
],
])('%s and sets the source to `route`', (_, url, params, result) => {
instrumentAngularRouting(customStartTransaction);
// this event starts the transaction
routerEvents$.next(new NavigationStart(0, url));
expect(customStartTransaction).toHaveBeenCalledWith({
name: url,
op: 'navigation',
metadata: { source: 'url' },
});
// this event starts the parameterization
routerEvents$.next(new ResolveEnd(1, url, url, { root: { params, children: [] } } as any));
expect(transaction.setName).toHaveBeenCalledWith(result);
expect(transaction.setMetadata).toHaveBeenCalledWith({ source: 'route' });
});
it('does not change the transaction name if the source is something other than `url`', () => {
instrumentAngularRouting(customStartTransaction);
const url = '/user/12345/test';
routerEvents$.next(new NavigationStart(0, url));
expect(customStartTransaction).toHaveBeenCalledWith({
name: url,
op: 'navigation',
metadata: { source: 'url' },
});
// Simulate that this transaction has a custom name:
transaction.metadata.source = 'custom';
// this event starts the parameterization
routerEvents$.next(new ResolveEnd(1, url, url, { root: { params: { userId: '12345' }, children: [] } } as any));
expect(transaction.setName).toHaveBeenCalledTimes(0);
expect(transaction.setMetadata).toHaveBeenCalledTimes(0);
expect(transaction.name).toEqual(url);
});
});
});
});