1
+ import { getCurrentHub } from '@sentry/hub' ;
2
+ import * as SentryReact from '@sentry/react' ;
1
3
import { Integrations as TracingIntegrations } from '@sentry/tracing' ;
2
4
import { Integration } from '@sentry/types' ;
5
+ import { getGlobalObject } from '@sentry/utils' ;
3
6
4
- import { init , Integrations , nextRouterInstrumentation , Scope } from '../src/index.client' ;
7
+ import { init , Integrations , nextRouterInstrumentation } from '../src/index.client' ;
5
8
import { NextjsOptions } from '../src/utils/nextjsOptions' ;
6
9
7
10
const { BrowserTracing } = TracingIntegrations ;
8
11
9
- const mockInit = jest . fn ( ) ;
10
- let configureScopeCallback : ( scope : Scope ) => void = ( ) => undefined ;
11
-
12
- jest . mock ( '@sentry/react' , ( ) => {
13
- const actual = jest . requireActual ( '@sentry/react' ) ;
14
- return {
15
- ...actual ,
16
- init : ( options : NextjsOptions ) => {
17
- mockInit ( options ) ;
18
- } ,
19
- configureScope : ( callback : ( scope : Scope ) => void ) => {
20
- configureScopeCallback = callback ;
21
- } ,
22
- } ;
23
- } ) ;
12
+ const global = getGlobalObject ( ) ;
13
+
14
+ const reactInit = jest . spyOn ( SentryReact , 'init' ) ;
24
15
25
16
describe ( 'Client init()' , ( ) => {
26
17
afterEach ( ( ) => {
27
- mockInit . mockClear ( ) ;
28
- configureScopeCallback = ( ) => undefined ;
18
+ reactInit . mockClear ( ) ;
19
+ global . __SENTRY__ . hub = undefined ;
29
20
} ) ;
30
21
31
22
it ( 'inits the React SDK' , ( ) => {
32
- expect ( mockInit ) . toHaveBeenCalledTimes ( 0 ) ;
23
+ expect ( reactInit ) . toHaveBeenCalledTimes ( 0 ) ;
33
24
init ( { } ) ;
34
- expect ( mockInit ) . toHaveBeenCalledTimes ( 1 ) ;
35
- expect ( mockInit ) . toHaveBeenLastCalledWith ( {
36
- _metadata : {
37
- sdk : {
38
- name : 'sentry.javascript.nextjs' ,
39
- version : expect . any ( String ) ,
40
- packages : expect . any ( Array ) ,
25
+ expect ( reactInit ) . toHaveBeenCalledTimes ( 1 ) ;
26
+ expect ( reactInit ) . toHaveBeenCalledWith (
27
+ expect . objectContaining ( {
28
+ _metadata : {
29
+ sdk : {
30
+ name : 'sentry.javascript.nextjs' ,
31
+ version : expect . any ( String ) ,
32
+ packages : expect . any ( Array ) ,
33
+ } ,
41
34
} ,
42
- } ,
43
- environment : 'test' ,
44
- integrations : undefined ,
45
- } ) ;
35
+ environment : 'test' ,
36
+ integrations : undefined ,
37
+ } ) ,
38
+ ) ;
46
39
} ) ;
47
40
48
41
it ( 'sets runtime on scope' , ( ) => {
49
- const mockScope = new Scope ( ) ;
42
+ const currentScope = getCurrentHub ( ) . getScope ( ) ;
43
+
44
+ // @ts -ignore need access to protected _tags attribute
45
+ expect ( currentScope . _tags ) . toEqual ( { } ) ;
46
+
50
47
init ( { } ) ;
51
- configureScopeCallback ( mockScope ) ;
48
+
52
49
// @ts -ignore need access to protected _tags attribute
53
- expect ( mockScope . _tags ) . toEqual ( { runtime : 'browser' } ) ;
50
+ expect ( currentScope . _tags ) . toEqual ( { runtime : 'browser' } ) ;
54
51
} ) ;
55
52
56
53
describe ( 'integrations' , ( ) => {
57
54
it ( 'does not add BrowserTracing integration by default if tracesSampleRate is not set' , ( ) => {
58
55
init ( { } ) ;
59
56
60
- const reactInitOptions : NextjsOptions = mockInit . mock . calls [ 0 ] [ 0 ] ;
57
+ const reactInitOptions : NextjsOptions = reactInit . mock . calls [ 0 ] [ 0 ] ;
61
58
expect ( reactInitOptions . integrations ) . toBeUndefined ( ) ;
62
59
} ) ;
63
60
64
61
it ( 'adds BrowserTracing integration by default if tracesSampleRate is set' , ( ) => {
65
62
init ( { tracesSampleRate : 1.0 } ) ;
66
63
67
- const reactInitOptions : NextjsOptions = mockInit . mock . calls [ 0 ] [ 0 ] ;
64
+ const reactInitOptions : NextjsOptions = reactInit . mock . calls [ 0 ] [ 0 ] ;
68
65
expect ( reactInitOptions . integrations ) . toHaveLength ( 1 ) ;
69
66
70
67
const integrations = reactInitOptions . integrations as Integration [ ] ;
@@ -78,7 +75,7 @@ describe('Client init()', () => {
78
75
it ( 'adds BrowserTracing integration by default if tracesSampler is set' , ( ) => {
79
76
init ( { tracesSampler : ( ) => true } ) ;
80
77
81
- const reactInitOptions : NextjsOptions = mockInit . mock . calls [ 0 ] [ 0 ] ;
78
+ const reactInitOptions : NextjsOptions = reactInit . mock . calls [ 0 ] [ 0 ] ;
82
79
expect ( reactInitOptions . integrations ) . toHaveLength ( 1 ) ;
83
80
84
81
const integrations = reactInitOptions . integrations as Integration [ ] ;
@@ -91,7 +88,7 @@ describe('Client init()', () => {
91
88
92
89
it ( 'supports passing integration through options' , ( ) => {
93
90
init ( { tracesSampleRate : 1.0 , integrations : [ new Integrations . Breadcrumbs ( { console : false } ) ] } ) ;
94
- const reactInitOptions : NextjsOptions = mockInit . mock . calls [ 0 ] [ 0 ] ;
91
+ const reactInitOptions : NextjsOptions = reactInit . mock . calls [ 0 ] [ 0 ] ;
95
92
expect ( reactInitOptions . integrations ) . toHaveLength ( 2 ) ;
96
93
97
94
const integrations = reactInitOptions . integrations as Integration [ ] ;
@@ -104,7 +101,7 @@ describe('Client init()', () => {
104
101
integrations : [ new BrowserTracing ( { idleTimeout : 5000 , startTransactionOnLocationChange : false } ) ] ,
105
102
} ) ;
106
103
107
- const reactInitOptions : NextjsOptions = mockInit . mock . calls [ 0 ] [ 0 ] ;
104
+ const reactInitOptions : NextjsOptions = reactInit . mock . calls [ 0 ] [ 0 ] ;
108
105
expect ( reactInitOptions . integrations ) . toHaveLength ( 1 ) ;
109
106
const integrations = reactInitOptions . integrations as Integration [ ] ;
110
107
expect ( ( integrations [ 0 ] as InstanceType < typeof BrowserTracing > ) . options ) . toEqual (
@@ -122,7 +119,7 @@ describe('Client init()', () => {
122
119
integrations : ( ) => [ new BrowserTracing ( { idleTimeout : 5000 , startTransactionOnLocationChange : false } ) ] ,
123
120
} ) ;
124
121
125
- const reactInitOptions : NextjsOptions = mockInit . mock . calls [ 0 ] [ 0 ] ;
122
+ const reactInitOptions : NextjsOptions = reactInit . mock . calls [ 0 ] [ 0 ] ;
126
123
const integrationFunc = reactInitOptions . integrations as ( ) => Integration [ ] ;
127
124
const integrations = integrationFunc ( ) ;
128
125
expect ( ( integrations [ 0 ] as InstanceType < typeof BrowserTracing > ) . options ) . toEqual (
0 commit comments