/
metrics.test.ts
205 lines (176 loc) · 6.17 KB
/
metrics.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
import { Span, Transaction } from '../../src';
import {
_startChild,
addResourceSpans,
DEFAULT_METRICS_INSTR_OPTIONS,
MetricsInstrumentation,
ResourceEntry,
} from '../../src/browser/metrics';
import { addDOMPropertiesToGlobal } from '../testutils';
// eslint-disable-next-line @typescript-eslint/no-explicit-any, no-var
declare var global: any;
describe('_startChild()', () => {
it('creates a span with given properties', () => {
const transaction = new Transaction({ name: 'test' });
const span = _startChild(transaction, {
description: 'evaluation',
op: 'script',
});
expect(span).toBeInstanceOf(Span);
expect(span.description).toBe('evaluation');
expect(span.op).toBe('script');
});
it('adjusts the start timestamp if child span starts before transaction', () => {
const transaction = new Transaction({ name: 'test', startTimestamp: 123 });
const span = _startChild(transaction, {
description: 'script.js',
op: 'resource',
startTimestamp: 100,
});
expect(transaction.startTimestamp).toEqual(span.startTimestamp);
expect(transaction.startTimestamp).toEqual(100);
});
it('does not adjust start timestamp if child span starts after transaction', () => {
const transaction = new Transaction({ name: 'test', startTimestamp: 123 });
const span = _startChild(transaction, {
description: 'script.js',
op: 'resource',
startTimestamp: 150,
});
expect(transaction.startTimestamp).not.toEqual(span.startTimestamp);
expect(transaction.startTimestamp).toEqual(123);
});
});
describe('addResourceSpans', () => {
const transaction = new Transaction({ name: 'hello' });
beforeEach(() => {
transaction.startChild = jest.fn();
});
// We already track xhr, we don't need to use
it('does not create spans for xmlhttprequest', () => {
const entry: ResourceEntry = {
initiatorType: 'xmlhttprequest',
transferSize: 256,
encodedBodySize: 256,
decodedBodySize: 256,
};
addResourceSpans(transaction, entry, '/assets/to/me', 123, 456, 100);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(transaction.startChild).toHaveBeenCalledTimes(0);
});
it('does not create spans for fetch', () => {
const entry: ResourceEntry = {
initiatorType: 'fetch',
transferSize: 256,
encodedBodySize: 256,
decodedBodySize: 256,
};
addResourceSpans(transaction, entry, '/assets/to/me', 123, 456, 100);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(transaction.startChild).toHaveBeenCalledTimes(0);
});
it('creates spans for resource spans', () => {
const entry: ResourceEntry = {
initiatorType: 'css',
transferSize: 256,
encodedBodySize: 456,
decodedBodySize: 593,
};
const timeOrigin = 100;
const startTime = 23;
const duration = 356;
const endTimestamp = addResourceSpans(transaction, entry, '/assets/to/css', startTime, duration, timeOrigin);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(transaction.startChild).toHaveBeenCalledTimes(1);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(transaction.startChild).toHaveBeenLastCalledWith({
data: {
['Decoded Body Size']: entry.decodedBodySize,
['Encoded Body Size']: entry.encodedBodySize,
['Transfer Size']: entry.transferSize,
},
description: '/assets/to/css',
endTimestamp: timeOrigin + startTime + duration,
op: 'resource.css',
startTimestamp: timeOrigin + startTime,
});
expect(endTimestamp).toBe(timeOrigin + startTime + duration);
});
it('creates a variety of resource spans', () => {
const table = [
{
initiatorType: undefined,
op: 'resource',
},
{
initiatorType: '',
op: 'resource',
},
{
initiatorType: 'css',
op: 'resource.css',
},
{
initiatorType: 'image',
op: 'resource.image',
},
{
initiatorType: 'script',
op: 'resource.script',
},
];
for (const { initiatorType, op } of table) {
const entry: ResourceEntry = {
initiatorType,
};
addResourceSpans(transaction, entry, '/assets/to/me', 123, 234, 465);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(transaction.startChild).toHaveBeenLastCalledWith(
expect.objectContaining({
op,
}),
);
}
});
it('allows for enter size of 0', () => {
const entry: ResourceEntry = {
initiatorType: 'css',
transferSize: 0,
encodedBodySize: 0,
decodedBodySize: 0,
};
addResourceSpans(transaction, entry, '/assets/to/css', 100, 23, 345);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(transaction.startChild).toHaveBeenCalledTimes(1);
// eslint-disable-next-line @typescript-eslint/unbound-method
expect(transaction.startChild).toHaveBeenLastCalledWith(
expect.objectContaining({
data: {
['Decoded Body Size']: entry.decodedBodySize,
['Encoded Body Size']: entry.encodedBodySize,
['Transfer Size']: entry.transferSize,
},
}),
);
});
});
describe('MetricsInstrumentation', () => {
it('does not initialize trackers when on node', () => {
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
);
new MetricsInstrumentation(DEFAULT_METRICS_INSTR_OPTIONS);
trackers.forEach(tracker => expect(tracker).not.toBeCalled());
});
it('initializes trackers when not on node and `global.performance` is available.', () => {
addDOMPropertiesToGlobal(['performance', 'document', 'addEventListener', 'window']);
const backup = global.process;
global.process = undefined;
const trackers = ['_trackCLS', '_trackLCP', '_trackFID'].map(tracker =>
jest.spyOn(MetricsInstrumentation.prototype as any, tracker),
);
new MetricsInstrumentation(DEFAULT_METRICS_INSTR_OPTIONS);
global.process = backup;
trackers.forEach(tracker => expect(tracker).toBeCalled());
});
});