1
1
import { SDK_VERSION } from '@sentry/core' ;
2
- import { expect } from 'chai' ;
3
- import { SinonSpy , spy } from 'sinon' ;
4
2
5
3
import {
6
4
addBreadcrumb ,
@@ -22,13 +20,13 @@ import { SimpleTransport } from './mocks/simpletransport';
22
20
23
21
const dsn = 'https://[email protected] /4291' ;
24
22
25
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, no-var
23
+ // eslint-disable-next-line no-var
26
24
declare var global : any ;
27
25
28
26
describe ( 'SentryBrowser' , ( ) => {
29
- const beforeSend : SinonSpy < [ Event ] , Event > = spy ( ( event : Event ) => event ) ;
27
+ const beforeSend = jest . fn ( ) ;
30
28
31
- before ( ( ) => {
29
+ beforeAll ( ( ) => {
32
30
init ( {
33
31
beforeSend,
34
32
dsn,
@@ -42,15 +40,15 @@ describe('SentryBrowser', () => {
42
40
43
41
afterEach ( ( ) => {
44
42
getCurrentHub ( ) . popScope ( ) ;
45
- beforeSend . resetHistory ( ) ;
43
+ beforeSend . mockReset ( ) ;
46
44
} ) ;
47
45
48
46
describe ( 'getContext() / setContext()' , ( ) => {
49
47
it ( 'should store/load extra' , ( ) => {
50
48
configureScope ( ( scope : Scope ) => {
51
49
scope . setExtra ( 'abc' , { def : [ 1 ] } ) ;
52
50
} ) ;
53
- expect ( global . __SENTRY__ . hub . _stack [ 1 ] . scope . _extra ) . to . deep . equal ( {
51
+ expect ( global . __SENTRY__ . hub . _stack [ 1 ] . scope . _extra ) . toEqual ( {
54
52
abc : { def : [ 1 ] } ,
55
53
} ) ;
56
54
} ) ;
@@ -59,7 +57,7 @@ describe('SentryBrowser', () => {
59
57
configureScope ( ( scope : Scope ) => {
60
58
scope . setTag ( 'abc' , 'def' ) ;
61
59
} ) ;
62
- expect ( global . __SENTRY__ . hub . _stack [ 1 ] . scope . _tags ) . to . deep . equal ( {
60
+ expect ( global . __SENTRY__ . hub . _stack [ 1 ] . scope . _tags ) . toEqual ( {
63
61
abc : 'def' ,
64
62
} ) ;
65
63
} ) ;
@@ -68,7 +66,7 @@ describe('SentryBrowser', () => {
68
66
configureScope ( ( scope : Scope ) => {
69
67
scope . setUser ( { id : 'def' } ) ;
70
68
} ) ;
71
- expect ( global . __SENTRY__ . hub . _stack [ 1 ] . scope . _user ) . to . deep . equal ( {
69
+ expect ( global . __SENTRY__ . hub . _stack [ 1 ] . scope . _user ) . toEqual ( {
72
70
id : 'def' ,
73
71
} ) ;
74
72
} ) ;
@@ -78,7 +76,11 @@ describe('SentryBrowser', () => {
78
76
describe ( 'user' , ( ) => {
79
77
const EX_USER = { email :
'[email protected] ' } ;
80
78
const client = new BrowserClient ( { dsn } ) ;
81
- spy ( client , 'showReportDialog' ) ;
79
+ const reportDialogSpy = jest . spyOn ( client , 'showReportDialog' ) ;
80
+
81
+ beforeEach ( ( ) => {
82
+ reportDialogSpy . mockReset ( ) ;
83
+ } ) ;
82
84
83
85
it ( 'uses the user on the scope' , ( ) => {
84
86
configureScope ( scope => {
@@ -88,8 +90,8 @@ describe('SentryBrowser', () => {
88
90
89
91
showReportDialog ( ) ;
90
92
91
- expect ( ( client . showReportDialog as SinonSpy ) . called ) . to . be . true ;
92
- expect ( ( client . showReportDialog as SinonSpy ) . lastCall . args [ 0 ] . user . email ) . to . eq ( EX_USER . email ) ;
93
+ expect ( reportDialogSpy ) . toBeCalled ( ) ;
94
+ expect ( reportDialogSpy . mock . calls [ 0 ] [ 0 ] ! . user ! . email ) . toBe ( EX_USER . email ) ;
93
95
} ) ;
94
96
95
97
it ( 'prioritizes options user over scope user' , ( ) => {
@@ -101,8 +103,8 @@ describe('SentryBrowser', () => {
101
103
const DIALOG_OPTION_USER = { email :
'[email protected] ' } ;
102
104
showReportDialog ( { user : DIALOG_OPTION_USER } ) ;
103
105
104
- expect ( ( client . showReportDialog as SinonSpy ) . called ) . to . be . true ;
105
- expect ( ( client . showReportDialog as SinonSpy ) . lastCall . args [ 0 ] . user . email ) . to . eq ( DIALOG_OPTION_USER . email ) ;
106
+ expect ( reportDialogSpy ) . toBeCalled ( ) ;
107
+ expect ( reportDialogSpy . mock . calls [ 0 ] [ 0 ] ! . user ! . email ) . toBe ( DIALOG_OPTION_USER . email ) ;
106
108
} ) ;
107
109
} ) ;
108
110
} ) ;
@@ -114,7 +116,7 @@ describe('SentryBrowser', () => {
114
116
115
117
captureMessage ( 'event' ) ;
116
118
await flush ( 2000 ) ;
117
- expect ( beforeSend . args [ 0 ] [ 0 ] . breadcrumbs ) . to . have . lengthOf ( 2 ) ;
119
+ expect ( beforeSend . mock . calls [ 0 ] [ 0 ] . breadcrumbs ) . toHaveLength ( 2 ) ;
118
120
} ) ;
119
121
} ) ;
120
122
@@ -128,22 +130,20 @@ describe('SentryBrowser', () => {
128
130
129
131
await flush ( 2000 ) ;
130
132
131
- const event = beforeSend . args [ 0 ] [ 0 ] ;
132
- expect ( event . exception ) . to . not . be . undefined ;
133
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
134
- expect ( event . exception ! . values ! [ 0 ] ) . to . not . be . undefined ;
135
- expect ( event . exception ! . values ! [ 0 ] . type ) . to . equal ( 'Error' ) ;
136
- expect ( event . exception ! . values ! [ 0 ] . value ) . to . equal ( 'test' ) ;
137
- expect ( event . exception ! . values ! [ 0 ] . stacktrace ) . to . not . be . empty ;
138
- /* eslint-enable @typescript-eslint/no-non-null-assertion */
133
+ const event = beforeSend . mock . calls [ 0 ] [ 0 ] ;
134
+ expect ( event . exception ) . toBeDefined ( ) ;
135
+ expect ( event . exception . values [ 0 ] ) . toBeDefined ( ) ;
136
+ expect ( event . exception . values [ 0 ] . type ) . toBe ( 'Error' ) ;
137
+ expect ( event . exception . values [ 0 ] . value ) . toBe ( 'test' ) ;
138
+ expect ( event . exception . values [ 0 ] . stacktrace . frames ) . not . toHaveLength ( 0 ) ;
139
139
} ) ;
140
140
141
141
it ( 'should capture a message' , done => {
142
142
getCurrentHub ( ) . bindClient (
143
143
new BrowserClient ( {
144
144
beforeSend : ( event : Event ) : Event | null => {
145
- expect ( event . message ) . to . equal ( 'test' ) ;
146
- expect ( event . exception ) . to . be . undefined ;
145
+ expect ( event . message ) . toBe ( 'test' ) ;
146
+ expect ( event . exception ) . toBeUndefined ( ) ;
147
147
done ( ) ;
148
148
return event ;
149
149
} ,
@@ -157,8 +157,8 @@ describe('SentryBrowser', () => {
157
157
getCurrentHub ( ) . bindClient (
158
158
new BrowserClient ( {
159
159
beforeSend : ( event : Event ) : Event | null => {
160
- expect ( event . message ) . to . equal ( 'event' ) ;
161
- expect ( event . exception ) . to . be . undefined ;
160
+ expect ( event . message ) . toBe ( 'event' ) ;
161
+ expect ( event . exception ) . toBeUndefined ( ) ;
162
162
done ( ) ;
163
163
return event ;
164
164
} ,
@@ -169,7 +169,7 @@ describe('SentryBrowser', () => {
169
169
} ) ;
170
170
171
171
it ( 'should not dedupe an event on bound client' , async ( ) => {
172
- const localBeforeSend = spy ( ) ;
172
+ const localBeforeSend = jest . fn ( ) ;
173
173
getCurrentHub ( ) . bindClient (
174
174
new BrowserClient ( {
175
175
beforeSend : localBeforeSend ,
@@ -183,11 +183,11 @@ describe('SentryBrowser', () => {
183
183
184
184
await flush ( 10 ) ;
185
185
186
- expect ( localBeforeSend . calledTwice ) . to . be . true ;
186
+ expect ( localBeforeSend ) . toHaveBeenCalledTimes ( 2 ) ;
187
187
} ) ;
188
188
189
189
it ( 'should use inboundfilter rules of bound client' , async ( ) => {
190
- const localBeforeSend = spy ( ) ;
190
+ const localBeforeSend = jest . fn ( ) ;
191
191
getCurrentHub ( ) . bindClient (
192
192
new BrowserClient ( {
193
193
beforeSend : localBeforeSend ,
@@ -200,7 +200,7 @@ describe('SentryBrowser', () => {
200
200
201
201
await flush ( 2000 ) ;
202
202
203
- expect ( localBeforeSend . called ) . to . be . false ;
203
+ expect ( localBeforeSend ) . not . toHaveBeenCalled ( ) ;
204
204
} ) ;
205
205
} ) ;
206
206
} ) ;
@@ -209,20 +209,20 @@ describe('SentryBrowser initialization', () => {
209
209
it ( 'should use window.SENTRY_RELEASE to set release on initialization if available' , ( ) => {
210
210
global . SENTRY_RELEASE = { id : 'foobar' } ;
211
211
init ( { dsn } ) ;
212
- expect ( global . __SENTRY__ . hub . _stack [ 0 ] . client . getOptions ( ) . release ) . to . equal ( 'foobar' ) ;
212
+ expect ( global . __SENTRY__ . hub . _stack [ 0 ] . client . getOptions ( ) . release ) . toBe ( 'foobar' ) ;
213
213
delete global . SENTRY_RELEASE ;
214
214
} ) ;
215
215
216
216
it ( 'should use initialScope' , ( ) => {
217
217
init ( { dsn, initialScope : { tags : { a : 'b' } } } ) ;
218
- expect ( global . __SENTRY__ . hub . _stack [ 0 ] . scope . _tags ) . to . deep . equal ( { a : 'b' } ) ;
218
+ expect ( global . __SENTRY__ . hub . _stack [ 0 ] . scope . _tags ) . toEqual ( { a : 'b' } ) ;
219
219
} ) ;
220
220
221
221
it ( 'should use initialScope Scope' , ( ) => {
222
222
const scope = new Scope ( ) ;
223
223
scope . setTags ( { a : 'b' } ) ;
224
224
init ( { dsn, initialScope : scope } ) ;
225
- expect ( global . __SENTRY__ . hub . _stack [ 0 ] . scope . _tags ) . to . deep . equal ( { a : 'b' } ) ;
225
+ expect ( global . __SENTRY__ . hub . _stack [ 0 ] . scope . _tags ) . toEqual ( { a : 'b' } ) ;
226
226
} ) ;
227
227
228
228
it ( 'should use initialScope callback' , ( ) => {
@@ -233,38 +233,36 @@ describe('SentryBrowser initialization', () => {
233
233
return scope ;
234
234
} ,
235
235
} ) ;
236
- expect ( global . __SENTRY__ . hub . _stack [ 0 ] . scope . _tags ) . to . deep . equal ( { a : 'b' } ) ;
236
+ expect ( global . __SENTRY__ . hub . _stack [ 0 ] . scope . _tags ) . toEqual ( { a : 'b' } ) ;
237
237
} ) ;
238
238
239
239
it ( 'should have initialization proceed as normal if window.SENTRY_RELEASE is not set' , ( ) => {
240
240
// This is mostly a happy-path test to ensure that the initialization doesn't throw an error.
241
241
init ( { dsn } ) ;
242
- expect ( global . __SENTRY__ . hub . _stack [ 0 ] . client . getOptions ( ) . release ) . to . be . undefined ;
242
+ expect ( global . __SENTRY__ . hub . _stack [ 0 ] . client . getOptions ( ) . release ) . toBeUndefined ( ) ;
243
243
} ) ;
244
244
245
245
describe ( 'SDK metadata' , ( ) => {
246
246
it ( 'should set SDK data when Sentry.init() is called' , ( ) => {
247
247
init ( { dsn } ) ;
248
248
249
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
250
249
const sdkData = ( getCurrentHub ( ) . getClient ( ) as any ) . _backend . _transport . _api . metadata ?. sdk ;
251
250
252
- expect ( sdkData . name ) . to . equal ( 'sentry.javascript.browser' ) ;
253
- expect ( sdkData . packages [ 0 ] . name ) . to . equal ( 'npm:@sentry/browser' ) ;
254
- expect ( sdkData . packages [ 0 ] . version ) . to . equal ( SDK_VERSION ) ;
255
- expect ( sdkData . version ) . to . equal ( SDK_VERSION ) ;
251
+ expect ( sdkData . name ) . toBe ( 'sentry.javascript.browser' ) ;
252
+ expect ( sdkData . packages [ 0 ] . name ) . toBe ( 'npm:@sentry/browser' ) ;
253
+ expect ( sdkData . packages [ 0 ] . version ) . toBe ( SDK_VERSION ) ;
254
+ expect ( sdkData . version ) . toBe ( SDK_VERSION ) ;
256
255
} ) ;
257
256
258
257
it ( 'should set SDK data when instantiating a client directly' , ( ) => {
259
258
const client = new BrowserClient ( { dsn } ) ;
260
259
261
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
262
260
const sdkData = ( client as any ) . _backend . _transport . _api . metadata ?. sdk ;
263
261
264
- expect ( sdkData . name ) . to . equal ( 'sentry.javascript.browser' ) ;
265
- expect ( sdkData . packages [ 0 ] . name ) . to . equal ( 'npm:@sentry/browser' ) ;
266
- expect ( sdkData . packages [ 0 ] . version ) . to . equal ( SDK_VERSION ) ;
267
- expect ( sdkData . version ) . to . equal ( SDK_VERSION ) ;
262
+ expect ( sdkData . name ) . toBe ( 'sentry.javascript.browser' ) ;
263
+ expect ( sdkData . packages [ 0 ] . name ) . toBe ( 'npm:@sentry/browser' ) ;
264
+ expect ( sdkData . packages [ 0 ] . version ) . toBe ( SDK_VERSION ) ;
265
+ expect ( sdkData . version ) . toBe ( SDK_VERSION ) ;
268
266
} ) ;
269
267
270
268
// wrapper packages (like @sentry/angular and @sentry/react) set their SDK data in their `init` methods, which are
@@ -287,13 +285,12 @@ describe('SentryBrowser initialization', () => {
287
285
} ,
288
286
} ) ;
289
287
290
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
291
288
const sdkData = ( getCurrentHub ( ) . getClient ( ) as any ) . _backend . _transport . _api . metadata ?. sdk ;
292
289
293
- expect ( sdkData . name ) . to . equal ( 'sentry.javascript.angular' ) ;
294
- expect ( sdkData . packages [ 0 ] . name ) . to . equal ( 'npm:@sentry/angular' ) ;
295
- expect ( sdkData . packages [ 0 ] . version ) . to . equal ( SDK_VERSION ) ;
296
- expect ( sdkData . version ) . to . equal ( SDK_VERSION ) ;
290
+ expect ( sdkData . name ) . toBe ( 'sentry.javascript.angular' ) ;
291
+ expect ( sdkData . packages [ 0 ] . name ) . toBe ( 'npm:@sentry/angular' ) ;
292
+ expect ( sdkData . packages [ 0 ] . version ) . toBe ( SDK_VERSION ) ;
293
+ expect ( sdkData . version ) . toBe ( SDK_VERSION ) ;
297
294
} ) ;
298
295
} ) ;
299
296
} ) ;
@@ -303,25 +300,27 @@ describe('wrap()', () => {
303
300
getCurrentHub ( ) . bindClient (
304
301
new BrowserClient ( {
305
302
beforeSend : ( event : Event ) : Event | null => {
306
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
307
- expect ( event . exception ! . values ! [ 0 ] . type ) . to . equal ( 'TypeError' ) ;
308
- expect ( event . exception ! . values ! [ 0 ] . value ) . to . equal ( 'mkey' ) ;
309
- /* eslint-enable @typescript-eslint/no-non-null-assertion */
303
+ expect ( event . exception ! . values ! [ 0 ] . type ) . toBe ( 'TypeError' ) ;
304
+ expect ( event . exception ! . values ! [ 0 ] . value ) . toBe ( 'mkey' ) ;
310
305
done ( ) ;
311
306
return null ;
312
307
} ,
313
308
dsn,
314
309
} ) ,
315
310
) ;
316
311
317
- wrap ( ( ) => {
318
- throw new TypeError ( 'mkey' ) ;
319
- } ) ;
312
+ try {
313
+ wrap ( ( ) => {
314
+ throw new TypeError ( 'mkey' ) ;
315
+ } ) ;
316
+ } catch ( e ) {
317
+ // no-empty
318
+ }
320
319
} ) ;
321
320
322
321
it ( 'should return result of a function call' , ( ) => {
323
322
const result = wrap ( ( ) => 2 ) ;
324
- expect ( result ) . to . equal ( 2 ) ;
323
+ expect ( result ) . toBe ( 2 ) ;
325
324
} ) ;
326
325
327
326
it ( 'should allow for passing this and arguments through binding' , ( ) => {
@@ -331,16 +330,16 @@ describe('wrap()', () => {
331
330
} . bind ( { context : 'this' } , 'b' , 42 ) ,
332
331
) ;
333
332
334
- expect ( ( result as unknown [ ] ) [ 0 ] ) . to . deep . equal ( { context : 'this' } ) ;
335
- expect ( ( result as unknown [ ] ) [ 1 ] ) . to . equal ( 'b' ) ;
336
- expect ( ( result as unknown [ ] ) [ 2 ] ) . to . equal ( 42 ) ;
333
+ expect ( ( result as unknown [ ] ) [ 0 ] ) . toEqual ( { context : 'this' } ) ;
334
+ expect ( ( result as unknown [ ] ) [ 1 ] ) . toBe ( 'b' ) ;
335
+ expect ( ( result as unknown [ ] ) [ 2 ] ) . toBe ( 42 ) ;
337
336
338
337
const result2 = wrap (
339
338
function ( this : { x : number } ) : number {
340
339
return this . x ;
341
340
} . bind ( { x : 42 } ) ,
342
341
) ;
343
342
344
- expect ( result2 ) . to . equal ( 42 ) ;
343
+ expect ( result2 ) . toBe ( 42 ) ;
345
344
} ) ;
346
345
} ) ;
0 commit comments