33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44import 'jsdom-global/register' ;
55import * as Sentry from '@sentry/browser' ;
6- import sentryMetrics , { _Sentry } from './browser' ;
6+ import * as sentryWrapper from './browser' ;
77import { SentryConfigOpts } from './models/SentryConfigOpts' ;
8- import { Logger } from './sentry.types' ;
98
10- const sinon = require ( 'sinon' ) ;
11- const sandbox = sinon . createSandbox ( ) ;
9+ jest . mock ( '@sentry/browser' , ( ) => {
10+ const actual = jest . requireActual ( '@sentry/browser' ) ;
11+ return {
12+ ...actual ,
13+ init : jest . fn ( ) ,
14+ captureException : jest . fn ( ) ,
15+ } ;
16+ } ) ;
1217
1318const config : SentryConfigOpts = {
1419 release : 'v0.0.0' ,
@@ -19,45 +24,41 @@ const config: SentryConfigOpts = {
1924 sampleRate : 0 ,
2025 } ,
2126} ;
22- const logger : Logger = {
27+ const logger = {
2328 info : jest . fn ( ) ,
2429 warn : jest . fn ( ) ,
2530 error : jest . fn ( ) ,
2631 debug : jest . fn ( ) ,
2732} ;
2833
2934describe ( 'sentry/browser' , ( ) => {
30- beforeAll ( ( ) => {
31- // Reduce console log noise in test output
32- sandbox . spy ( console , 'error' ) ;
33- } ) ;
34-
3535 beforeEach ( ( ) => {
3636 // Make sure it's enabled by default
37- sentryMetrics . enable ( ) ;
37+ sentryWrapper . enable ( ) ;
3838 } ) ;
3939
40- afterAll ( ( ) => {
41- sandbox . restore ( ) ;
40+ afterEach ( ( ) => {
41+ jest . resetAllMocks ( ) ;
42+ jest . resetModules ( ) ;
4243 } ) ;
4344
4445 describe ( 'init' , ( ) => {
4546 it ( 'properly configures with dsn' , ( ) => {
46- sentryMetrics . configure ( config , logger ) ;
47+ sentryWrapper . configure ( config , logger ) ;
4748 } ) ;
4849 } ) ;
4950
5051 describe ( 'beforeSend' , ( ) => {
5152 beforeAll ( ( ) => {
52- sentryMetrics . configure ( config , logger ) ;
53+ sentryWrapper . configure ( config , logger ) ;
5354 } ) ;
5455
5556 it ( 'works without request url' , ( ) => {
5657 const data = {
5758 key : 'value' ,
5859 } as Sentry . Event ;
5960
60- const resultData = sentryMetrics . __beforeSend ( config , data , { } ) ;
61+ const resultData = sentryWrapper . beforeSend ( config , data , { } ) ;
6162
6263 expect ( data ) . toEqual ( resultData ) ;
6364 } ) ;
@@ -72,35 +73,34 @@ describe('sentry/browser', () => {
7273 } ,
7374 } as Sentry . Event ;
7475
75- const resultData = sentryMetrics . __beforeSend ( config , data , { } ) ;
76+ const resultData = sentryWrapper . beforeSend ( config , data , { } ) ;
7677 expect ( resultData ?. fingerprint ?. [ 0 ] ) . toEqual ( 'errno100' ) ;
7778 expect ( resultData ?. level ) . toEqual ( 'info' ) ;
7879 } ) ;
7980 } ) ;
8081
8182 describe ( 'captureException' , ( ) => {
8283 it ( 'calls Sentry.captureException' , ( ) => {
83- const sentryCaptureException = sinon . stub ( _Sentry , 'captureException' ) ;
84- sentryMetrics . captureException ( new Error ( 'testo' ) ) ;
85- sinon . assert . calledOnce ( sentryCaptureException ) ;
86- sentryCaptureException . restore ( ) ;
84+ const spy = jest . spyOn ( Sentry , 'captureException' ) ;
85+ Sentry . captureException ( new Error ( 'testo' ) ) ;
86+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
8787 } ) ;
8888 } ) ;
8989
9090 describe ( 'disable / enables' , ( ) => {
9191 it ( 'enables' , ( ) => {
92- sentryMetrics . enable ( ) ;
93- expect ( sentryMetrics . __sentryEnabled ( ) ) . toBeTruthy ( ) ;
92+ sentryWrapper . enable ( ) ;
93+ expect ( sentryWrapper . isEnabled ( ) ) . toBeTruthy ( ) ;
9494 } ) ;
9595
9696 it ( 'disables' , ( ) => {
97- sentryMetrics . disable ( ) ;
98- expect ( sentryMetrics . __sentryEnabled ( ) ) . toBeFalsy ( ) ;
97+ sentryWrapper . disable ( ) ;
98+ expect ( sentryWrapper . isEnabled ( ) ) . toBeFalsy ( ) ;
9999 } ) ;
100100
101101 it ( 'will return null from before send when disabled' , ( ) => {
102- sentryMetrics . disable ( ) ;
103- expect ( sentryMetrics . __beforeSend ( { } , { } , { } ) ) . toBeNull ( ) ;
102+ sentryWrapper . disable ( ) ;
103+ expect ( sentryWrapper . beforeSend ( { } , { } , { } ) ) . toBeNull ( ) ;
104104 } ) ;
105105 } ) ;
106106} ) ;
0 commit comments