@@ -16,14 +16,46 @@ describe("Span", () => {
16
16
} )
17
17
18
18
it ( "returns the span when no error is given" , ( ) => {
19
- span . setError ( ( undefined as unknown ) as Error )
19
+ const result = span . setError ( ( undefined as unknown ) as Error )
20
20
expect ( span . serialize ( ) . error . message ) . toEqual ( "No error has been set" )
21
+ expect ( result ) . toBe ( span )
21
22
} )
22
23
23
24
it ( "returns the span when the passed object is not an error" , ( ) => {
24
25
const event = new CloseEvent ( "event" )
25
- span . setError ( ( event as unknown ) as Error )
26
+ const result = span . setError ( ( event as unknown ) as Error )
26
27
expect ( span . serialize ( ) . error . message ) . toEqual ( "No error has been set" )
28
+ expect ( result ) . toBe ( span )
29
+ } )
30
+ } )
31
+
32
+ describe ( "setEnvironment" , ( ) => {
33
+ it ( "updates the spans environment" , ( ) => {
34
+ span . setEnvironment ( { key : "value" } )
35
+ expect ( span . serialize ( ) . environment ) . toEqual ( { key : "value" } )
36
+ } )
37
+
38
+ it ( "merges the environment" , ( ) => {
39
+ span . setEnvironment ( { key : "value" } )
40
+ span . setEnvironment ( { key2 : "value2" } )
41
+ expect ( span . serialize ( ) . environment ) . toEqual ( {
42
+ key : "value" ,
43
+ key2 : "value2"
44
+ } )
45
+ } )
46
+
47
+ it ( "returns the span when no environment is given" , ( ) => {
48
+ const result = span . setEnvironment (
49
+ ( undefined as unknown ) as { key : string }
50
+ )
51
+ expect ( span . serialize ( ) . environment ) . toEqual ( { } )
52
+ expect ( result ) . toBe ( span )
53
+ } )
54
+
55
+ it ( "returns the span when the passed object is not an object" , ( ) => {
56
+ const result = span . setEnvironment ( ( 123 as unknown ) as { key : string } )
57
+ expect ( span . serialize ( ) . environment ) . toEqual ( { } )
58
+ expect ( result ) . toBe ( span )
27
59
} )
28
60
} )
29
61
@@ -49,6 +81,10 @@ describe("Span", () => {
49
81
" at track (http://thirdparty.app/script.js:1:530)" ,
50
82
" at app/bundle.js:21:10"
51
83
] )
84
+
85
+ expect ( span . serialize ( ) . environment ) . toMatchObject ( {
86
+ backtrace_paths_matched : "3"
87
+ } )
52
88
} )
53
89
54
90
it ( "cleans Safari/FF-style backtraces" , ( ) => {
@@ -70,6 +106,10 @@ describe("Span", () => {
70
106
"track@http://thirdparty.app/script.js:1:530" ,
71
107
"@app/bundle.js:21:10"
72
108
] )
109
+
110
+ expect ( span . serialize ( ) . environment ) . toMatchObject ( {
111
+ backtrace_paths_matched : "3"
112
+ } )
73
113
} )
74
114
75
115
it ( "concatenates all match groups" , ( ) => {
@@ -95,6 +135,10 @@ describe("Span", () => {
95
135
" at track (http://thirdparty.app/script.js:1:530)" ,
96
136
" at assets/app/bundle.js:21:10"
97
137
] )
138
+
139
+ expect ( span . serialize ( ) . environment ) . toMatchObject ( {
140
+ backtrace_paths_matched : "3"
141
+ } )
98
142
} )
99
143
100
144
it ( "tries matchers in order" , ( ) => {
@@ -119,6 +163,71 @@ describe("Span", () => {
119
163
"Foo@app/bundle.js:13:10" ,
120
164
"Bar@app/bundle.js:17:10"
121
165
] )
166
+
167
+ expect ( span . serialize ( ) . environment ) . toMatchObject ( {
168
+ backtrace_paths_matched : "2"
169
+ } )
170
+ } )
171
+
172
+ it ( "does not match any paths when the matcher has no match groups" , ( ) => {
173
+ const error = new Error ( "test error" )
174
+ error . stack = [
175
+ "Foo@http://localhost:8080/assets/app/bundle.js:13:10"
176
+ ] . join ( "\n" )
177
+
178
+ span . setError ( error )
179
+ // This regex always matches the line, but contains no match groups,
180
+ // meaning the replacement that the matcher would do would be to an
181
+ // empty string.
182
+ //
183
+ // This should result in the line not being modified.
184
+ span . cleanBacktracePath ( new RegExp ( ".*" ) )
185
+
186
+ const backtrace = span . serialize ( ) . error . backtrace
187
+ expect ( backtrace ) . toEqual ( [
188
+ "Foo@http://localhost:8080/assets/app/bundle.js:13:10"
189
+ ] )
190
+
191
+ expect ( span . serialize ( ) . environment ) . toBeUndefined ( )
192
+ } )
193
+
194
+ it ( "does not replace the path when the match is empty" , ( ) => {
195
+ const error = new Error ( "test error" )
196
+ error . stack = [
197
+ "Foo@http://localhost:8080/assets/app/bundle.js:13:10"
198
+ ] . join ( "\n" )
199
+
200
+ span . setError ( error )
201
+ // This regex always matches the line with an empty match group,
202
+ // meaning the replacement that the matcher would do would be to an
203
+ // empty string.
204
+ //
205
+ // This should result in the line not being modified.
206
+ span . cleanBacktracePath ( new RegExp ( ".*(z*)$" ) )
207
+
208
+ const backtrace = span . serialize ( ) . error . backtrace
209
+ expect ( backtrace ) . toEqual ( [
210
+ "Foo@http://localhost:8080/assets/app/bundle.js:13:10"
211
+ ] )
212
+
213
+ expect ( span . serialize ( ) . environment ) . toBeUndefined ( )
214
+ } )
215
+
216
+ it ( "does not report `backtrace_paths_matched` when no paths are matched" , ( ) => {
217
+ const error = new Error ( "test error" )
218
+ error . stack = [
219
+ "Foo@http://localhost:8080/assets/app/bundle.js:13:10"
220
+ ] . join ( "\n" )
221
+
222
+ span . setError ( error )
223
+ span . cleanBacktracePath ( new RegExp ( "^pancakes/(.*)$" ) )
224
+
225
+ const backtrace = span . serialize ( ) . error . backtrace
226
+ expect ( backtrace ) . toEqual ( [
227
+ "Foo@http://localhost:8080/assets/app/bundle.js:13:10"
228
+ ] )
229
+
230
+ expect ( span . serialize ( ) . environment ) . toBeUndefined ( )
122
231
} )
123
232
} )
124
233
} )
0 commit comments