11import { defaultStackParser } from '../../src' ;
2- import { eventFromPlainObject } from '../../src/eventbuilder' ;
2+ import { eventFromUnknownInput } from '../../src/eventbuilder' ;
33
44jest . mock ( '@sentry/core' , ( ) => {
55 const original = jest . requireActual ( '@sentry/core' ) ;
@@ -12,17 +12,6 @@ jest.mock('@sentry/core', () => {
1212 } ,
1313 } ;
1414 } ,
15- getCurrentHub ( ) {
16- return {
17- getClient ( ) : any {
18- return {
19- getOptions ( ) : any {
20- return { normalizeDepth : 6 } ;
21- } ,
22- } ;
23- } ,
24- } ;
25- } ,
2615 } ;
2716} ) ;
2817
@@ -35,7 +24,7 @@ afterEach(() => {
3524 jest . resetAllMocks ( ) ;
3625} ) ;
3726
38- describe ( 'eventFromPlainObject ' , ( ) => {
27+ describe ( 'eventFromUnknownInput ' , ( ) => {
3928 it ( 'should use normalizeDepth from init options' , ( ) => {
4029 const deepObject = {
4130 a : {
@@ -53,7 +42,7 @@ describe('eventFromPlainObject', () => {
5342 } ,
5443 } ;
5544
56- const event = eventFromPlainObject ( defaultStackParser , deepObject ) ;
45+ const event = eventFromUnknownInput ( defaultStackParser , deepObject ) ;
5746
5847 expect ( event ?. extra ?. __serialized__ ) . toEqual ( {
5948 a : {
@@ -71,16 +60,107 @@ describe('eventFromPlainObject', () => {
7160 } ) ;
7261
7362 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 ] [ ] ) (
8099 '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 ) ;
83102 expect ( actual . exception ?. values ?. [ 0 ] ?. value ) . toEqual ( expected ) ;
103+
104+ expect ( actual . extra ) . toEqual ( {
105+ __serialized__ : serializedException ,
106+ } ) ;
84107 } ,
85108 ) ;
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+ } ) ;
86166} ) ;
0 commit comments