Skip to content

Commit 2c30234

Browse files
committed
Testing.
1 parent a245269 commit 2c30234

File tree

4 files changed

+502
-18
lines changed

4 files changed

+502
-18
lines changed

packages/telemetry/browser-telemetry/__tests__/options.test.ts

Lines changed: 380 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import ErrorCollector from '../src/collectors/error';
22
import parse, { defaultOptions } from '../src/options';
33

4+
const mockLogger = {
5+
warn: jest.fn(),
6+
};
7+
8+
beforeEach(() => {
9+
jest.clearAllMocks();
10+
});
11+
412
it('handles an empty configuration', () => {
513
const outOptions = parse({});
614
expect(outOptions).toEqual(defaultOptions());
715
});
816

9-
it('can set each option', () => {
17+
it('can set all options at once', () => {
1018
const outOptions = parse({
1119
maxPendingEvents: 1,
1220
breadcrumbs: {
@@ -41,3 +49,374 @@ it('can set each option', () => {
4149
collectors: [new ErrorCollector(), new ErrorCollector()],
4250
});
4351
});
52+
53+
it('warns when maxPendingEvents is not a number', () => {
54+
const outOptions = parse(
55+
{
56+
// @ts-ignore
57+
maxPendingEvents: 'not a number',
58+
},
59+
mockLogger,
60+
);
61+
62+
expect(outOptions.maxPendingEvents).toEqual(defaultOptions().maxPendingEvents);
63+
expect(mockLogger.warn).toHaveBeenCalledWith(
64+
'Config option "maxPendingEvents" should be of type number, got string, using default value',
65+
);
66+
});
67+
68+
it('accepts valid maxPendingEvents number', () => {
69+
const outOptions = parse(
70+
{
71+
maxPendingEvents: 100,
72+
},
73+
mockLogger,
74+
);
75+
76+
expect(outOptions.maxPendingEvents).toEqual(100);
77+
expect(mockLogger.warn).not.toHaveBeenCalled();
78+
});
79+
80+
it('warns when breadcrumbs config is not an object', () => {
81+
const outOptions = parse(
82+
{
83+
// @ts-ignore
84+
breadcrumbs: 'not an object',
85+
},
86+
mockLogger,
87+
);
88+
89+
expect(outOptions.breadcrumbs).toEqual(defaultOptions().breadcrumbs);
90+
expect(mockLogger.warn).toHaveBeenCalledWith(
91+
'Config option "breadcrumbs" should be of type object, got string, using default value',
92+
);
93+
});
94+
95+
it('warns when collectors is not an array', () => {
96+
const outOptions = parse(
97+
{
98+
// @ts-ignore
99+
collectors: 'not an array',
100+
},
101+
mockLogger,
102+
);
103+
104+
expect(outOptions.collectors).toEqual(defaultOptions().collectors);
105+
expect(mockLogger.warn).toHaveBeenCalledWith(
106+
'Config option "collectors" should be of type Collector[], got string, using default value',
107+
);
108+
});
109+
110+
it('accepts valid collectors array', () => {
111+
const collectors = [new ErrorCollector()];
112+
const outOptions = parse(
113+
{
114+
collectors,
115+
},
116+
mockLogger,
117+
);
118+
119+
expect(outOptions.collectors).toEqual(collectors);
120+
expect(mockLogger.warn).not.toHaveBeenCalled();
121+
});
122+
123+
it('warns when stack config is not an object', () => {
124+
const outOptions = parse(
125+
{
126+
// @ts-ignore
127+
stack: 'not an object',
128+
},
129+
mockLogger,
130+
);
131+
132+
expect(outOptions.stack).toEqual(defaultOptions().stack);
133+
expect(mockLogger.warn).toHaveBeenCalledWith(
134+
'Config option "stack" should be of type object, got string, using default value',
135+
);
136+
});
137+
138+
it('warns when breadcrumbs.maxBreadcrumbs is not a number', () => {
139+
const outOptions = parse(
140+
{
141+
breadcrumbs: {
142+
// @ts-ignore
143+
maxBreadcrumbs: 'not a number',
144+
},
145+
},
146+
mockLogger,
147+
);
148+
149+
expect(outOptions.breadcrumbs.maxBreadcrumbs).toEqual(
150+
defaultOptions().breadcrumbs.maxBreadcrumbs,
151+
);
152+
expect(mockLogger.warn).toHaveBeenCalledWith(
153+
'Config option "breadcrumbs.maxBreadcrumbs" should be of type number, got string, using default value',
154+
);
155+
});
156+
157+
it('accepts valid breadcrumbs.maxBreadcrumbs number', () => {
158+
const outOptions = parse(
159+
{
160+
breadcrumbs: {
161+
maxBreadcrumbs: 50,
162+
},
163+
},
164+
mockLogger,
165+
);
166+
167+
expect(outOptions.breadcrumbs.maxBreadcrumbs).toEqual(50);
168+
expect(mockLogger.warn).not.toHaveBeenCalled();
169+
});
170+
171+
it('warns when breadcrumbs.click is not boolean', () => {
172+
const outOptions = parse(
173+
{
174+
breadcrumbs: {
175+
// @ts-ignore
176+
click: 'not a boolean',
177+
},
178+
},
179+
mockLogger,
180+
);
181+
182+
expect(outOptions.breadcrumbs.click).toEqual(defaultOptions().breadcrumbs.click);
183+
expect(mockLogger.warn).toHaveBeenCalledWith(
184+
'Config option "breadcrumbs.click" should be of type boolean, got string, using default value',
185+
);
186+
});
187+
188+
it('warns when breadcrumbs.evaluations is not boolean', () => {
189+
const outOptions = parse(
190+
{
191+
breadcrumbs: {
192+
// @ts-ignore
193+
evaluations: 'not a boolean',
194+
},
195+
},
196+
mockLogger,
197+
);
198+
199+
expect(outOptions.breadcrumbs.evaluations).toEqual(defaultOptions().breadcrumbs.evaluations);
200+
expect(mockLogger.warn).toHaveBeenCalledWith(
201+
'Config option "breadcrumbs.evaluations" should be of type boolean, got string, using default value',
202+
);
203+
});
204+
205+
it('warns when breadcrumbs.flagChange is not boolean', () => {
206+
const outOptions = parse(
207+
{
208+
breadcrumbs: {
209+
// @ts-ignore
210+
flagChange: 'not a boolean',
211+
},
212+
},
213+
mockLogger,
214+
);
215+
216+
expect(outOptions.breadcrumbs.flagChange).toEqual(defaultOptions().breadcrumbs.flagChange);
217+
expect(mockLogger.warn).toHaveBeenCalledWith(
218+
'Config option "breadcrumbs.flagChange" should be of type boolean, got string, using default value',
219+
);
220+
});
221+
222+
it('warns when breadcrumbs.keyboardInput is not boolean', () => {
223+
const outOptions = parse(
224+
{
225+
breadcrumbs: {
226+
// @ts-ignore
227+
keyboardInput: 'not a boolean',
228+
},
229+
},
230+
mockLogger,
231+
);
232+
233+
expect(outOptions.breadcrumbs.keyboardInput).toEqual(defaultOptions().breadcrumbs.keyboardInput);
234+
expect(mockLogger.warn).toHaveBeenCalledWith(
235+
'Config option "breadcrumbs.keyboardInput" should be of type boolean, got string, using default value',
236+
);
237+
});
238+
239+
it('accepts valid breadcrumbs.click boolean', () => {
240+
const outOptions = parse(
241+
{
242+
breadcrumbs: {
243+
click: false,
244+
},
245+
},
246+
mockLogger,
247+
);
248+
249+
expect(outOptions.breadcrumbs.click).toEqual(false);
250+
expect(mockLogger.warn).not.toHaveBeenCalled();
251+
});
252+
253+
it('accepts valid breadcrumbs.evaluations boolean', () => {
254+
const outOptions = parse(
255+
{
256+
breadcrumbs: {
257+
evaluations: false,
258+
},
259+
},
260+
mockLogger,
261+
);
262+
263+
expect(outOptions.breadcrumbs.evaluations).toEqual(false);
264+
expect(mockLogger.warn).not.toHaveBeenCalled();
265+
});
266+
267+
it('accepts valid breadcrumbs.flagChange boolean', () => {
268+
const outOptions = parse(
269+
{
270+
breadcrumbs: {
271+
flagChange: false,
272+
},
273+
},
274+
mockLogger,
275+
);
276+
277+
expect(outOptions.breadcrumbs.flagChange).toEqual(false);
278+
expect(mockLogger.warn).not.toHaveBeenCalled();
279+
});
280+
281+
it('accepts valid breadcrumbs.keyboardInput boolean', () => {
282+
const outOptions = parse(
283+
{
284+
breadcrumbs: {
285+
keyboardInput: false,
286+
},
287+
},
288+
mockLogger,
289+
);
290+
291+
expect(outOptions.breadcrumbs.keyboardInput).toEqual(false);
292+
expect(mockLogger.warn).not.toHaveBeenCalled();
293+
});
294+
295+
it('warns when breadcrumbs.http is not an object', () => {
296+
const outOptions = parse(
297+
{
298+
breadcrumbs: {
299+
// @ts-ignore
300+
http: 'not an object',
301+
},
302+
},
303+
mockLogger,
304+
);
305+
306+
expect(outOptions.breadcrumbs.http).toEqual(defaultOptions().breadcrumbs.http);
307+
expect(mockLogger.warn).toHaveBeenCalledWith(
308+
'Config option "breadcrumbs.http" should be of type HttpBreadCrumbOptions | false, got string, using default value',
309+
);
310+
});
311+
312+
it('warns when breadcrumbs.http.instrumentFetch is not boolean', () => {
313+
const outOptions = parse(
314+
{
315+
breadcrumbs: {
316+
http: {
317+
// @ts-ignore
318+
instrumentFetch: 'not a boolean',
319+
},
320+
},
321+
},
322+
mockLogger,
323+
);
324+
325+
expect(outOptions.breadcrumbs.http.instrumentFetch).toEqual(
326+
defaultOptions().breadcrumbs.http.instrumentFetch,
327+
);
328+
expect(mockLogger.warn).toHaveBeenCalledWith(
329+
'Config option "breadcrumbs.http.instrumentFetch" should be of type boolean, got string, using default value',
330+
);
331+
});
332+
333+
it('warns when breadcrumbs.http.instrumentXhr is not boolean', () => {
334+
const outOptions = parse(
335+
{
336+
breadcrumbs: {
337+
http: {
338+
// @ts-ignore
339+
instrumentXhr: 'not a boolean',
340+
},
341+
},
342+
},
343+
mockLogger,
344+
);
345+
346+
expect(outOptions.breadcrumbs.http.instrumentXhr).toEqual(
347+
defaultOptions().breadcrumbs.http.instrumentXhr,
348+
);
349+
expect(mockLogger.warn).toHaveBeenCalledWith(
350+
'Config option "breadcrumbs.http.instrumentXhr" should be of type boolean, got string, using default value',
351+
);
352+
});
353+
354+
it('accepts valid breadcrumbs.http.instrumentFetch boolean', () => {
355+
const outOptions = parse(
356+
{
357+
breadcrumbs: {
358+
http: {
359+
instrumentFetch: false,
360+
},
361+
},
362+
},
363+
mockLogger,
364+
);
365+
366+
expect(outOptions.breadcrumbs.http.instrumentFetch).toEqual(false);
367+
expect(mockLogger.warn).not.toHaveBeenCalled();
368+
});
369+
370+
it('accepts valid breadcrumbs.http.instrumentXhr boolean', () => {
371+
const outOptions = parse(
372+
{
373+
breadcrumbs: {
374+
http: {
375+
instrumentXhr: false,
376+
},
377+
},
378+
},
379+
mockLogger,
380+
);
381+
382+
expect(outOptions.breadcrumbs.http.instrumentXhr).toEqual(false);
383+
expect(mockLogger.warn).not.toHaveBeenCalled();
384+
});
385+
386+
it('accepts valid breadcrumbs.http.customUrlFilter function', () => {
387+
const outOptions = parse(
388+
{
389+
breadcrumbs: {
390+
http: {
391+
customUrlFilter: (url: string) => url.replace('secret', 'redacted'),
392+
},
393+
},
394+
},
395+
mockLogger,
396+
);
397+
398+
expect(outOptions.breadcrumbs.http.customUrlFilter).toBeDefined();
399+
expect(outOptions.breadcrumbs.http.customUrlFilter?.('test-secret-123')).toBe(
400+
'test-redacted-123',
401+
);
402+
expect(mockLogger.warn).not.toHaveBeenCalled();
403+
});
404+
405+
it('warns when breadcrumbs.http.customUrlFilter is not a function', () => {
406+
const outOptions = parse(
407+
{
408+
breadcrumbs: {
409+
http: {
410+
// @ts-ignore
411+
customUrlFilter: 'not a function',
412+
},
413+
},
414+
},
415+
mockLogger,
416+
);
417+
418+
expect(outOptions.breadcrumbs.http.customUrlFilter).toBeUndefined();
419+
expect(mockLogger.warn).toHaveBeenCalledWith(
420+
'The "breadcrumbs.http.customUrlFilter" must be a function. Received string',
421+
);
422+
});

0 commit comments

Comments
 (0)