-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathnormalize.test.ts
748 lines (662 loc) · 19.6 KB
/
normalize.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/**
* @jest-environment jsdom
*/
import * as isModule from '../src/is';
import { normalize } from '../src/normalize';
import { addNonEnumerableProperty } from '../src/object';
import * as stacktraceModule from '../src/stacktrace';
describe('normalize()', () => {
describe('acts as a pass-through for simple-cases', () => {
test('return same value for simple input', () => {
expect(normalize('foo')).toEqual('foo');
expect(normalize(42)).toEqual(42);
expect(normalize(true)).toEqual(true);
expect(normalize(null)).toEqual(null);
expect(normalize(undefined)).toBeUndefined();
});
test('return same object or arrays for referenced inputs', () => {
expect(normalize({ foo: 'bar' })).toEqual({ foo: 'bar' });
expect(normalize([42])).toEqual([42]);
});
});
describe('convertToPlainObject()', () => {
test('extracts extra properties from error objects', () => {
const obj = new Error('Wubba Lubba Dub Dub') as any;
obj.reason = new TypeError("I'm pickle Riiick!");
obj.extra = 'some extra prop';
obj.stack = 'x';
obj.reason.stack = 'x';
// IE 10/11
delete obj.description;
delete obj.reason.description;
expect(normalize(obj)).toEqual({
message: 'Wubba Lubba Dub Dub',
name: 'Error',
stack: 'x',
reason: {
message: "I'm pickle Riiick!",
name: 'TypeError',
stack: 'x',
},
extra: 'some extra prop',
});
});
describe('extracts data from `Event` objects', () => {
const isElement = jest.spyOn(isModule, 'isElement').mockReturnValue(true);
const getAttribute = () => undefined;
const parkElement = { tagName: 'PARK', getAttribute };
const treeElement = { tagName: 'TREE', parentNode: parkElement, getAttribute };
const squirrelElement = { tagName: 'SQUIRREL', parentNode: treeElement, getAttribute };
const chaseEvent = new Event('chase');
Object.defineProperty(chaseEvent, 'target', { value: squirrelElement });
Object.defineProperty(chaseEvent, 'currentTarget', { value: parkElement });
Object.defineProperty(chaseEvent, 'wagging', { value: true, enumerable: false });
expect(normalize(chaseEvent)).toEqual({
currentTarget: 'park',
isTrusted: false,
target: 'park > tree > squirrel',
type: 'chase',
// notice that `wagging` isn't included because it's not enumerable and not one of the ones we specifically extract
});
isElement.mockRestore();
});
});
describe('decycles cyclical structures', () => {
test('circular objects', () => {
const obj = { name: 'Alice' } as any;
obj.self = obj;
expect(normalize(obj)).toEqual({ name: 'Alice', self: '[Circular ~]' });
});
test('circular objects with intermediaries', () => {
const obj = { name: 'Alice' } as any;
obj.identity = { self: obj };
expect(normalize(obj)).toEqual({ name: 'Alice', identity: { self: '[Circular ~]' } });
});
test('circular objects with proxy', () => {
const obj1 = { name: 'Alice', child: null } as any;
const obj2 = { name: 'John', child: null } as any;
function getObj1(target: any, prop: string | number | symbol): any {
return prop === 'child'
? new Proxy(obj2, {
get(t, p) {
return getObj2(t, p);
},
})
: target[prop];
}
function getObj2(target: any, prop: string | number | symbol): any {
return prop === 'child'
? new Proxy(obj1, {
get(t, p) {
return getObj1(t, p);
},
})
: target[prop];
}
const proxy1 = new Proxy(obj1, {
get(target, prop) {
return getObj1(target, prop);
},
});
const actual = normalize(proxy1);
// This generates 100 nested objects, as we cannot identify the circular reference since they are dynamic proxies
// However, this test verifies that we can normalize at all, and do not fail out
expect(actual).toEqual({
name: 'Alice',
child: { name: 'John', child: expect.objectContaining({ name: 'Alice', child: expect.any(Object) }) },
});
let last = actual;
for (let i = 0; i < 99; i++) {
expect(last).toEqual(
expect.objectContaining({
name: expect.any(String),
child: expect.any(Object),
}),
);
last = last.child;
}
// Last one is transformed to [Object]
expect(last).toEqual(
expect.objectContaining({
name: expect.any(String),
child: '[Object]',
}),
);
});
test('deep circular objects', () => {
const obj = { name: 'Alice', child: { name: 'Bob' } } as any;
obj.child.self = obj.child;
expect(normalize(obj)).toEqual({
name: 'Alice',
child: { name: 'Bob', self: '[Circular ~]' },
});
});
test('deep circular objects with intermediaries', () => {
const obj = { name: 'Alice', child: { name: 'Bob' } } as any;
obj.child.identity = { self: obj.child };
expect(normalize(obj)).toEqual({
name: 'Alice',
child: { name: 'Bob', identity: { self: '[Circular ~]' } },
});
});
test('circular objects in an array', () => {
const obj = { name: 'Alice' } as any;
obj.self = [obj, obj];
expect(normalize(obj)).toEqual({
name: 'Alice',
self: ['[Circular ~]', '[Circular ~]'],
});
});
test('deep circular objects in an array', () => {
const obj = {
name: 'Alice',
children: [{ name: 'Bob' }, { name: 'Eve' }],
} as any;
obj.children[0]!.self = obj.children[0];
obj.children[1]!.self = obj.children[1];
expect(normalize(obj)).toEqual({
name: 'Alice',
children: [
{ name: 'Bob', self: '[Circular ~]' },
{ name: 'Eve', self: '[Circular ~]' },
],
});
});
test('circular arrays', () => {
const obj: object[] = [];
obj.push(obj);
obj.push(obj);
expect(normalize(obj)).toEqual(['[Circular ~]', '[Circular ~]']);
});
test('circular arrays with intermediaries', () => {
const obj: object[] = [];
obj.push({ name: 'Alice', self: obj });
obj.push({ name: 'Bob', self: obj });
expect(normalize(obj)).toEqual([
{ name: 'Alice', self: '[Circular ~]' },
{ name: 'Bob', self: '[Circular ~]' },
]);
});
test('repeated objects in objects', () => {
const obj = {} as any;
const alice = { name: 'Alice' };
obj.alice1 = alice;
obj.alice2 = alice;
expect(normalize(obj)).toEqual({
alice1: { name: 'Alice' },
alice2: { name: 'Alice' },
});
});
test('repeated objects in arrays', () => {
const alice = { name: 'Alice' };
const obj = [alice, alice];
expect(normalize(obj)).toEqual([{ name: 'Alice' }, { name: 'Alice' }]);
});
test('error objects with circular references', () => {
const obj = new Error('Wubba Lubba Dub Dub') as any;
obj.reason = obj;
obj.stack = 'x';
obj.reason.stack = 'x';
// IE 10/11
delete obj.description;
expect(normalize(obj)).toEqual({
message: 'Wubba Lubba Dub Dub',
name: 'Error',
stack: 'x',
reason: '[Circular ~]',
});
});
});
describe("doesn't mutate the given object and skips non-enumerables", () => {
test('simple object', () => {
const circular = {
foo: 1,
} as any;
circular.bar = circular;
const normalized = normalize(circular);
expect(normalized).toEqual({
foo: 1,
bar: '[Circular ~]',
});
expect(circular.bar).toBe(circular);
expect(normalized).not.toBe(circular);
});
test('complex object', () => {
const circular = {
foo: 1,
} as any;
circular.bar = [
{
baz: circular,
},
circular,
];
circular.qux = circular.bar[0]?.baz;
const normalized = normalize(circular);
expect(normalized).toEqual({
bar: [
{
baz: '[Circular ~]',
},
'[Circular ~]',
],
foo: 1,
qux: '[Circular ~]',
});
expect(circular.bar[0]?.baz).toBe(circular);
expect(circular.bar[1]).toBe(circular);
expect(circular.qux).toBe(circular.bar[0]?.baz);
expect(normalized).not.toBe(circular);
});
test('object with non-enumerable properties', () => {
const circular = {
foo: 1,
} as any;
circular.bar = circular;
circular.baz = {
one: 1337,
};
Object.defineProperty(circular, 'qux', {
enumerable: true,
value: circular,
});
Object.defineProperty(circular, 'quaz', {
enumerable: false,
value: circular,
});
Object.defineProperty(circular.baz, 'two', {
enumerable: false,
value: circular,
});
expect(normalize(circular)).toEqual({
bar: '[Circular ~]',
baz: {
one: 1337,
},
foo: 1,
qux: '[Circular ~]',
});
});
});
describe('handles HTML elements', () => {
test('HTMLDivElement', () => {
expect(
normalize({
div: document.createElement('div'),
div2: document.createElement('div'),
}),
).toEqual({
div: '[HTMLElement: HTMLDivElement]',
div2: '[HTMLElement: HTMLDivElement]',
});
});
test('input elements', () => {
expect(
normalize({
input: document.createElement('input'),
select: document.createElement('select'),
}),
).toEqual({
input: '[HTMLElement: HTMLInputElement]',
select: '[HTMLElement: HTMLSelectElement]',
});
});
});
describe('calls toJSON if implemented', () => {
test('primitive values', () => {
const a = new Number(1) as any;
a.toJSON = () => 10;
const b = new String('2') as any;
b.toJSON = () => '20';
expect(normalize(a)).toEqual(10);
expect(normalize(b)).toEqual('20');
});
test('objects, arrays and classes', () => {
const a = Object.create({});
a.toJSON = () => 1;
function B(): void {
/* no-empty */
}
B.prototype.toJSON = () => 2;
const c: any = [];
c.toJSON = () => 3;
// @ts-expect-error target lacks a construct signature
expect(normalize([{ a }, { b: new B() }, c])).toEqual([{ a: 1 }, { b: 2 }, 3]);
});
test('should return a normalized object even if a property was created without a prototype', () => {
const subject = { a: 1, foo: Object.create(null), bar: Object.assign(Object.create(null), { baz: true }) } as any;
expect(normalize(subject)).toEqual({ a: 1, foo: {}, bar: { baz: true } });
});
test('should return a normalized object even if toJSON throws', () => {
const subject = { a: 1, foo: 'bar' } as any;
subject.toJSON = () => {
throw new Error("I'm faulty!");
};
expect(normalize(subject)).toEqual({ a: 1, foo: 'bar', toJSON: '[Function: <anonymous>]' });
});
test('should return an object without circular references when toJSON returns an object with circular references', () => {
const subject: any = {};
subject.toJSON = () => {
const egg: any = {};
egg.chicken = egg;
return egg;
};
expect(normalize(subject)).toEqual({ chicken: '[Circular ~]' });
});
test('should detect circular reference when toJSON returns the original object', () => {
const subject: any = {};
subject.toJSON = () => subject;
expect(normalize(subject)).toEqual('[Circular ~]');
});
});
describe('changes unserializeable/global values/classes to their respective string representations', () => {
test('primitive values', () => {
expect(normalize(NaN)).toEqual('[NaN]');
expect(normalize(Infinity)).toEqual('[Infinity]');
expect(normalize(-Infinity)).toEqual('[-Infinity]');
expect(normalize(Symbol('dogs'))).toEqual('[Symbol(dogs)]');
expect(normalize(BigInt(1121201212312012))).toEqual('[BigInt: 1121201212312012]');
});
test('functions', () => {
expect(
normalize(() => {
/* no-empty */
}),
).toEqual('[Function: <anonymous>]');
const foo = () => {
/* no-empty */
};
expect(normalize(foo)).toEqual('[Function: foo]');
});
test('primitive values in objects/arrays', () => {
expect(normalize(['foo', 42, NaN])).toEqual(['foo', 42, '[NaN]']);
expect(
normalize({
foo: 42,
bar: NaN,
}),
).toEqual({
foo: 42,
bar: '[NaN]',
});
});
test('primitive values in deep objects/arrays', () => {
expect(normalize(['foo', 42, [[undefined]], [NaN]])).toEqual(['foo', 42, [[undefined]], ['[NaN]']]);
expect(
normalize({
foo: 42,
bar: {
baz: {
quz: null,
},
},
wat: {
no: NaN,
},
}),
).toEqual({
foo: 42,
bar: {
baz: {
quz: null,
},
},
wat: {
no: '[NaN]',
},
});
});
test("known classes like React's `SyntheticEvent`", () => {
const obj = {
foo: {
nativeEvent: 'wat',
preventDefault: 'wat',
stopPropagation: 'wat',
},
};
expect(normalize(obj)).toEqual({
foo: '[SyntheticEvent]',
});
});
test('known classes like `VueViewModel`', () => {
const obj = {
foo: {
_isVue: true,
},
};
expect(normalize(obj)).toEqual({
foo: '[VueViewModel]',
});
});
});
describe('can limit object to depth', () => {
test('single level', () => {
const obj = {
foo: [],
};
expect(normalize(obj, 1)).toEqual({
foo: '[Array]',
});
});
test('two levels', () => {
const obj = {
foo: [1, 2, []],
};
expect(normalize(obj, 2)).toEqual({
foo: [1, 2, '[Array]'],
});
});
test('multiple levels with various inputs', () => {
const obj = {
foo: {
bar: {
baz: 1,
qux: [
{
rick: 'morty',
},
],
},
},
bar: 1,
baz: [
{
something: 'else',
fn: () => {
/* no-empty */
},
},
],
};
expect(normalize(obj, 3)).toEqual({
bar: 1,
baz: [
{
something: 'else',
fn: '[Function: fn]',
},
],
foo: {
bar: {
baz: 1,
qux: '[Array]',
},
},
});
});
});
describe('can limit max properties', () => {
test('object', () => {
const obj = {
nope: 'here',
foo: {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
},
after: 'more',
};
expect(normalize(obj, 10, 5)).toEqual({
nope: 'here',
foo: {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: '[MaxProperties ~]',
},
after: 'more',
});
});
test('array', () => {
const obj = {
nope: 'here',
foo: new Array(100).fill('s'),
after: 'more',
};
expect(normalize(obj, 10, 5)).toEqual({
nope: 'here',
foo: ['s', 's', 's', 's', 's', '[MaxProperties ~]'],
after: 'more',
});
});
});
describe('handles serialization errors', () => {
test('restricts effect of error to problematic node', () => {
jest.spyOn(stacktraceModule, 'getFunctionName').mockImplementationOnce(() => {
throw new Error('Nope');
});
expect(normalize({ dogs: 'are great!', someFunc: () => {} })).toEqual({
dogs: 'are great!',
someFunc: '**non-serializable** (Error: Nope)',
});
});
});
test("normalizes value on every iteration of decycle and takes care of things like React's `SyntheticEvent`", () => {
const obj = {
foo: {
nativeEvent: 'wat',
preventDefault: 'wat',
stopPropagation: 'wat',
},
baz: NaN,
qux: function qux(): void {
/* no-empty */
},
};
const result = normalize(obj);
expect(result).toEqual({
foo: '[SyntheticEvent]',
baz: '[NaN]',
qux: '[Function: qux]',
});
});
test('normalizes value on every iteration of decycle and takes care of things like `VueViewModel`', () => {
const obj = {
foo: {
_isVue: true,
},
baz: NaN,
qux: function qux(): void {
/* no-empty */
},
};
const result = normalize(obj);
expect(result).toEqual({
foo: '[VueViewModel]',
baz: '[NaN]',
qux: '[Function: qux]',
});
});
describe('skips normalizing objects marked with a non-enumerable property __sentry_skip_normalization__', () => {
test('by leaving non-serializable values intact', () => {
const someFun = () => undefined;
const alreadyNormalizedObj = {
nan: NaN,
fun: someFun,
};
addNonEnumerableProperty(alreadyNormalizedObj, '__sentry_skip_normalization__', true);
const result = normalize(alreadyNormalizedObj);
expect(result).toEqual({
nan: NaN,
fun: someFun,
});
});
test('by ignoring normalization depth', () => {
const alreadyNormalizedObj = {
three: {
more: {
layers: '!',
},
},
};
addNonEnumerableProperty(alreadyNormalizedObj, '__sentry_skip_normalization__', true);
const obj = {
foo: {
bar: {
baz: alreadyNormalizedObj,
boo: {
bam: {
pow: 'poof',
},
},
},
},
};
const result = normalize(obj, 4);
expect(result?.foo?.bar?.baz?.three?.more?.layers).toBe('!');
expect(result?.foo?.bar?.boo?.bam?.pow).not.toBe('poof');
});
});
describe('overrides normalization depth with a non-enumerable property __sentry_override_normalization_depth__', () => {
test('by increasing depth if it is higher', () => {
const normalizationTarget = {
foo: 'bar',
baz: 42,
obj: {
obj: {
obj: {
bestSmashCharacter: 'Cpt. Falcon',
},
},
},
};
addNonEnumerableProperty(normalizationTarget, '__sentry_override_normalization_depth__', 3);
const result = normalize(normalizationTarget, 1);
expect(result).toEqual({
baz: 42,
foo: 'bar',
obj: {
obj: {
obj: '[Object]',
},
},
});
});
test('by decreasing depth if it is lower', () => {
const normalizationTarget = {
foo: 'bar',
baz: 42,
obj: {
obj: {
obj: {
bestSmashCharacter: 'Cpt. Falcon',
},
},
},
};
addNonEnumerableProperty(normalizationTarget, '__sentry_override_normalization_depth__', 1);
const result = normalize(normalizationTarget, 3);
expect(result).toEqual({
baz: 42,
foo: 'bar',
obj: '[Object]',
});
});
});
});