4
4
5
5
/* eslint-disable @typescript-eslint/unbound-method */
6
6
import type { Mock } from 'vitest' ;
7
- import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
7
+ import { afterEach , describe , expect , it , vi } from 'vitest' ;
8
8
9
9
import * as SentryCore from '@sentry/core' ;
10
10
import { createTransport } from '@sentry/core' ;
@@ -13,7 +13,7 @@ import type { Integration } from '@sentry/core';
13
13
14
14
import type { BrowserOptions } from '../src' ;
15
15
import { WINDOW } from '../src' ;
16
- import { applyDefaultOptions , init } from '../src/sdk' ;
16
+ import { applyDefaultOptions , init , initWithDefaultIntegrations } from '../src/sdk' ;
17
17
18
18
const PUBLIC_DSN = 'https://username@domain/123' ;
19
19
@@ -35,15 +35,11 @@ export class MockIntegration implements Integration {
35
35
}
36
36
37
37
describe ( 'init' , ( ) => {
38
- beforeEach ( ( ) => {
39
- vi . clearAllMocks ( ) ;
38
+ afterEach ( ( ) => {
39
+ vi . restoreAllMocks ( ) ;
40
40
} ) ;
41
41
42
- afterAll ( ( ) => {
43
- vi . resetAllMocks ( ) ;
44
- } ) ;
45
-
46
- test ( 'installs default integrations' , ( ) => {
42
+ test ( 'installs passed default integrations' , ( ) => {
47
43
const DEFAULT_INTEGRATIONS : Integration [ ] = [
48
44
new MockIntegration ( 'MockIntegration 0.1' ) ,
49
45
new MockIntegration ( 'MockIntegration 0.2' ) ,
@@ -56,28 +52,41 @@ describe('init', () => {
56
52
expect ( DEFAULT_INTEGRATIONS [ 1 ] ! . setupOnce as Mock ) . toHaveBeenCalledTimes ( 1 ) ;
57
53
} ) ;
58
54
55
+ it ( 'installs default integrations' , ( ) => {
56
+ // Note: We need to prevent this from actually adding all the default integrations, as otherwise
57
+ // following tests may fail (e.g. because console is monkey patched etc.)
58
+ const spyGetIntegrationsToSetup = vi . spyOn ( SentryCore , 'getIntegrationsToSetup' ) . mockImplementation ( ( ) => [ ] ) ;
59
+
60
+ const options = getDefaultBrowserOptions ( { dsn : PUBLIC_DSN } ) ;
61
+ init ( options ) ;
62
+
63
+ expect ( spyGetIntegrationsToSetup ) . toHaveBeenCalledTimes ( 1 ) ;
64
+ expect ( spyGetIntegrationsToSetup ) . toHaveBeenCalledWith (
65
+ expect . objectContaining ( options ) ,
66
+ expect . arrayContaining ( [ expect . objectContaining ( { name : 'InboundFilters' } ) ] ) ,
67
+ ) ;
68
+ } ) ;
69
+
59
70
it ( 'installs default integrations if `defaultIntegrations: undefined`' , ( ) => {
60
- // @ts -expect-error this is fine for testing
61
- const initAndBindSpy = vi . spyOn ( SentryCore , 'initAndBind' ) . mockImplementationOnce ( ( ) => { } ) ;
71
+ // Note: We need to prevent this from actually adding all the default integrations, as otherwise
72
+ // following tests may fail (e.g. because console is monkey patched etc.)
73
+ const spyGetIntegrationsToSetup = vi . spyOn ( SentryCore , 'getIntegrationsToSetup' ) . mockImplementation ( ( ) => [ ] ) ;
74
+
62
75
const options = getDefaultBrowserOptions ( { dsn : PUBLIC_DSN , defaultIntegrations : undefined } ) ;
63
76
init ( options ) ;
64
77
65
- expect ( initAndBindSpy ) . toHaveBeenCalledTimes ( 1 ) ;
66
-
67
- const optionsPassed = initAndBindSpy . mock . calls [ 0 ] ?. [ 1 ] ;
68
- expect ( optionsPassed ?. integrations . length ) . toBeGreaterThan ( 0 ) ;
78
+ expect ( spyGetIntegrationsToSetup ) . toHaveBeenCalledTimes ( 1 ) ;
79
+ expect ( spyGetIntegrationsToSetup ) . toHaveBeenCalledWith (
80
+ expect . objectContaining ( options ) ,
81
+ expect . arrayContaining ( [ expect . objectContaining ( { name : 'InboundFilters' } ) ] ) ,
82
+ ) ;
69
83
} ) ;
70
84
71
- test ( "doesn't install default integrations if told not to" , ( ) => {
72
- const DEFAULT_INTEGRATIONS : Integration [ ] = [
73
- new MockIntegration ( 'MockIntegration 0.3' ) ,
74
- new MockIntegration ( 'MockIntegration 0.4' ) ,
75
- ] ;
85
+ test ( "doesn't install any default integrations if told not to" , ( ) => {
76
86
const options = getDefaultBrowserOptions ( { dsn : PUBLIC_DSN , defaultIntegrations : false } ) ;
77
- init ( options ) ;
87
+ const client = init ( options ) ;
78
88
79
- expect ( DEFAULT_INTEGRATIONS [ 0 ] ! . setupOnce as Mock ) . toHaveBeenCalledTimes ( 0 ) ;
80
- expect ( DEFAULT_INTEGRATIONS [ 1 ] ! . setupOnce as Mock ) . toHaveBeenCalledTimes ( 0 ) ;
89
+ expect ( client ?. [ '_integrations' ] ) . toEqual ( { } ) ;
81
90
} ) ;
82
91
83
92
it ( 'installs merged default integrations, with overrides provided through options' , ( ) => {
@@ -137,7 +146,7 @@ describe('init', () => {
137
146
Object . defineProperty ( WINDOW , 'browser' , { value : undefined , writable : true } ) ;
138
147
Object . defineProperty ( WINDOW , 'nw' , { value : undefined , writable : true } ) ;
139
148
Object . defineProperty ( WINDOW , 'window' , { value : WINDOW , writable : true } ) ;
140
- vi . clearAllMocks ( ) ;
149
+ vi . restoreAllMocks ( ) ;
141
150
} ) ;
142
151
143
152
it ( 'logs a browser extension error if executed inside a Chrome extension' , ( ) => {
@@ -154,8 +163,6 @@ describe('init', () => {
154
163
expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
155
164
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/' ,
156
165
) ;
157
-
158
- consoleErrorSpy . mockRestore ( ) ;
159
166
} ) ;
160
167
161
168
it ( 'logs a browser extension error if executed inside a Firefox/Safari extension' , ( ) => {
@@ -169,8 +176,6 @@ describe('init', () => {
169
176
expect ( consoleErrorSpy ) . toHaveBeenCalledWith (
170
177
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/' ,
171
178
) ;
172
-
173
- consoleErrorSpy . mockRestore ( ) ;
174
179
} ) ;
175
180
176
181
it . each ( [ 'chrome-extension' , 'moz-extension' , 'ms-browser-extension' , 'safari-web-extension' ] ) (
@@ -249,6 +254,32 @@ describe('init', () => {
249
254
} ) ;
250
255
} ) ;
251
256
257
+ describe ( 'initWithDefaultIntegrations' , ( ) => {
258
+ afterEach ( ( ) => {
259
+ vi . restoreAllMocks ( ) ;
260
+ } ) ;
261
+
262
+ test ( 'installs with provided getDefaultIntegrations function' , ( ) => {
263
+ const integration1 = new MockIntegration ( SentryCore . uuid4 ( ) ) ;
264
+ const integration2 = new MockIntegration ( SentryCore . uuid4 ( ) ) ;
265
+ const getDefaultIntegrations = vi . fn ( ( ) => [ integration1 , integration2 ] ) ;
266
+ const options = getDefaultBrowserOptions ( { dsn : PUBLIC_DSN } ) ;
267
+
268
+ const client = initWithDefaultIntegrations ( options , getDefaultIntegrations ) ;
269
+
270
+ expect ( getDefaultIntegrations ) . toHaveBeenCalledTimes ( 1 ) ;
271
+ expect ( getDefaultIntegrations ) . toHaveBeenCalledWith ( options ) ;
272
+
273
+ expect ( client ) . toBeDefined ( ) ;
274
+ expect ( client ?. [ '_integrations' ] ) . toEqual ( {
275
+ [ integration1 . name ] : integration1 ,
276
+ [ integration2 . name ] : integration2 ,
277
+ } ) ;
278
+ expect ( integration1 . setupOnce ) . toHaveBeenCalledTimes ( 1 ) ;
279
+ expect ( integration2 . setupOnce ) . toHaveBeenCalledTimes ( 1 ) ;
280
+ } ) ;
281
+ } ) ;
282
+
252
283
describe ( 'applyDefaultOptions' , ( ) => {
253
284
test ( 'it works with empty options' , ( ) => {
254
285
const options = { } ;
0 commit comments