1
1
import { defaultStackParser } from '../../src' ;
2
- import { eventFromPlainObject } from '../../src/eventbuilder' ;
2
+ import { eventFromUnknownInput } from '../../src/eventbuilder' ;
3
3
4
4
jest . mock ( '@sentry/core' , ( ) => {
5
5
const original = jest . requireActual ( '@sentry/core' ) ;
@@ -12,17 +12,6 @@ jest.mock('@sentry/core', () => {
12
12
} ,
13
13
} ;
14
14
} ,
15
- getCurrentHub ( ) {
16
- return {
17
- getClient ( ) : any {
18
- return {
19
- getOptions ( ) : any {
20
- return { normalizeDepth : 6 } ;
21
- } ,
22
- } ;
23
- } ,
24
- } ;
25
- } ,
26
15
} ;
27
16
} ) ;
28
17
@@ -35,7 +24,7 @@ afterEach(() => {
35
24
jest . resetAllMocks ( ) ;
36
25
} ) ;
37
26
38
- describe ( 'eventFromPlainObject ' , ( ) => {
27
+ describe ( 'eventFromUnknownInput ' , ( ) => {
39
28
it ( 'should use normalizeDepth from init options' , ( ) => {
40
29
const deepObject = {
41
30
a : {
@@ -53,7 +42,7 @@ describe('eventFromPlainObject', () => {
53
42
} ,
54
43
} ;
55
44
56
- const event = eventFromPlainObject ( defaultStackParser , deepObject ) ;
45
+ const event = eventFromUnknownInput ( defaultStackParser , deepObject ) ;
57
46
58
47
expect ( event ?. extra ?. __serialized__ ) . toEqual ( {
59
48
a : {
@@ -71,16 +60,107 @@ describe('eventFromPlainObject', () => {
71
60
} ) ;
72
61
73
62
it . each ( [
74
- [ 'empty object' , { } , 'Object captured as exception with keys: [object has no keys]' ] ,
75
- [ 'pojo' , { prop1 : 'hello' , prop2 : 2 } , 'Object captured as exception with keys: prop1, prop2' ] ,
76
- [ 'Custom Class' , new MyTestClass ( ) , 'Object captured as exception with keys: prop1, prop2' ] ,
77
- [ 'Event' , new Event ( 'custom' ) , 'Event `Event` (type=custom) captured as exception' ] ,
78
- [ 'MouseEvent' , new MouseEvent ( 'click' ) , 'Event `MouseEvent` (type=click) captured as exception' ] ,
79
- ] as [ string , Record < string , unknown > , string ] [ ] ) (
63
+ [ 'empty object' , { } , { } , 'Object captured as exception with keys: [object has no keys]' ] ,
64
+ [
65
+ 'pojo' ,
66
+ { prop1 : 'hello' , prop2 : 2 } ,
67
+ { prop1 : 'hello' , prop2 : 2 } ,
68
+ 'Object captured as exception with keys: prop1, prop2' ,
69
+ ] ,
70
+ [
71
+ 'Custom Class' ,
72
+ new MyTestClass ( ) ,
73
+ { prop1 : 'hello' , prop2 : 2 } ,
74
+ 'Object captured as exception with keys: prop1, prop2' ,
75
+ ] ,
76
+ [
77
+ 'Event' ,
78
+ new Event ( 'custom' ) ,
79
+ {
80
+ currentTarget : '[object Null]' ,
81
+ isTrusted : false ,
82
+ target : '[object Null]' ,
83
+ type : 'custom' ,
84
+ } ,
85
+ 'Event `Event` (type=custom) captured as exception' ,
86
+ ] ,
87
+ [
88
+ 'MouseEvent' ,
89
+ new MouseEvent ( 'click' ) ,
90
+ {
91
+ currentTarget : '[object Null]' ,
92
+ isTrusted : false ,
93
+ target : '[object Null]' ,
94
+ type : 'click' ,
95
+ } ,
96
+ 'Event `MouseEvent` (type=click) captured as exception' ,
97
+ ] ,
98
+ ] as [ string , Record < string , unknown > , Record < string , unknown > , string ] [ ] ) (
80
99
'has correct exception value for %s' ,
81
- ( _name , exception , expected ) => {
82
- const actual = eventFromPlainObject ( defaultStackParser , exception ) ;
100
+ ( _name , exception , serializedException , expected ) => {
101
+ const actual = eventFromUnknownInput ( defaultStackParser , exception ) ;
83
102
expect ( actual . exception ?. values ?. [ 0 ] ?. value ) . toEqual ( expected ) ;
103
+
104
+ expect ( actual . extra ) . toEqual ( {
105
+ __serialized__ : serializedException ,
106
+ } ) ;
84
107
} ,
85
108
) ;
109
+
110
+ it ( 'handles object with error prop' , ( ) => {
111
+ const error = new Error ( 'Some error' ) ;
112
+ const event = eventFromUnknownInput ( defaultStackParser , {
113
+ foo : { bar : 'baz' } ,
114
+ name : 'BadType' ,
115
+ err : error ,
116
+ } ) ;
117
+
118
+ expect ( event . exception ?. values ?. [ 0 ] ) . toEqual (
119
+ expect . objectContaining ( {
120
+ mechanism : { handled : true , synthetic : true , type : 'generic' } ,
121
+ type : 'Error' ,
122
+ value : 'Some error' ,
123
+ } ) ,
124
+ ) ;
125
+ expect ( event . extra ) . toEqual ( {
126
+ __serialized__ : {
127
+ foo : { bar : 'baz' } ,
128
+ name : 'BadType' ,
129
+ err : {
130
+ message : 'Some error' ,
131
+ name : 'Error' ,
132
+ stack : expect . stringContaining ( 'Error: Some error' ) ,
133
+ } ,
134
+ } ,
135
+ } ) ;
136
+ } ) ;
137
+
138
+ it ( 'handles class with error prop' , ( ) => {
139
+ const error = new Error ( 'Some error' ) ;
140
+
141
+ class MyTestClass {
142
+ prop1 = 'hello' ;
143
+ prop2 = error ;
144
+ }
145
+
146
+ const event = eventFromUnknownInput ( defaultStackParser , new MyTestClass ( ) ) ;
147
+
148
+ expect ( event . exception ?. values ?. [ 0 ] ) . toEqual (
149
+ expect . objectContaining ( {
150
+ mechanism : { handled : true , synthetic : true , type : 'generic' } ,
151
+ type : 'Error' ,
152
+ value : 'Some error' ,
153
+ } ) ,
154
+ ) ;
155
+ expect ( event . extra ) . toEqual ( {
156
+ __serialized__ : {
157
+ prop1 : 'hello' ,
158
+ prop2 : {
159
+ message : 'Some error' ,
160
+ name : 'Error' ,
161
+ stack : expect . stringContaining ( 'Error: Some error' ) ,
162
+ } ,
163
+ } ,
164
+ } ) ;
165
+ } ) ;
86
166
} ) ;
0 commit comments