File tree 5 files changed +63
-9
lines changed
dev-packages/browser-integration-tests/suites/integrations/lazyLoad/validIntegrationNpm
5 files changed +63
-9
lines changed Original file line number Diff line number Diff line change
1
+ import * as Sentry from '@sentry/browser' ;
2
+
3
+ // So we can use this in subject.js
4
+ // We specifically DO NOT set this on window.Sentry as we want to test a non-CDN bundle environment,
5
+ // where window.Sentry is usually not available
6
+ window . _testSentry = { ...Sentry } ;
7
+
8
+ Sentry . init ( {
9
+ dsn :
'https://[email protected] /1337' ,
10
+ integrations : [ ] ,
11
+ } ) ;
Original file line number Diff line number Diff line change
1
+ window . _testLazyLoadIntegration = async function run ( ) {
2
+ const integration = await window . _testSentry . lazyLoadIntegration ( 'httpClientIntegration' ) ;
3
+
4
+ window . _testSentry . getClient ( ) ?. addIntegration ( integration ( ) ) ;
5
+
6
+ window . _integrationLoaded = true ;
7
+ } ;
Original file line number Diff line number Diff line change
1
+ import { expect } from '@playwright/test' ;
2
+ import { SDK_VERSION } from '@sentry/browser' ;
3
+
4
+ import { sentryTest } from '../../../../utils/fixtures' ;
5
+
6
+ sentryTest ( 'it allows to lazy load an integration when using the NPM package' , async ( { getLocalTestUrl, page } ) => {
7
+ const bundle = process . env . PW_BUNDLE || '' ;
8
+ // We only want to run this in non-CDN bundle mode
9
+ if ( bundle . startsWith ( 'bundle' ) ) {
10
+ sentryTest . skip ( ) ;
11
+ }
12
+
13
+ const url = await getLocalTestUrl ( { testDir : __dirname } ) ;
14
+
15
+ await page . route ( `https://browser.sentry-cdn.com/${ SDK_VERSION } /httpclient.min.js` , route => {
16
+ return route . fulfill ( {
17
+ status : 200 ,
18
+ contentType : 'application/javascript;' ,
19
+ body : "window.Sentry = window.Sentry || {};window.Sentry.httpClientIntegration = () => ({ name: 'HttpClient' })" ,
20
+ } ) ;
21
+ } ) ;
22
+
23
+ await page . goto ( url ) ;
24
+
25
+ const hasIntegration = await page . evaluate ( '!!window._testSentry.getClient().getIntegrationByName("HttpClient")' ) ;
26
+ expect ( hasIntegration ) . toBe ( false ) ;
27
+
28
+ const scriptTagsBefore = await page . evaluate < number > ( 'document.querySelectorAll("script").length' ) ;
29
+
30
+ await page . evaluate ( 'window._testLazyLoadIntegration()' ) ;
31
+ await page . waitForFunction ( 'window._integrationLoaded' ) ;
32
+
33
+ const scriptTagsAfter = await page . evaluate < number > ( 'document.querySelectorAll("script").length' ) ;
34
+
35
+ const hasIntegration2 = await page . evaluate ( '!!window._testSentry.getClient().getIntegrationByName("HttpClient")' ) ;
36
+ expect ( hasIntegration2 ) . toBe ( true ) ;
37
+
38
+ expect ( scriptTagsAfter ) . toBe ( scriptTagsBefore + 1 ) ;
39
+ } ) ;
Original file line number Diff line number Diff line change @@ -33,12 +33,15 @@ const WindowWithMaybeIntegration = WINDOW as {
33
33
export async function lazyLoadIntegration ( name : keyof typeof LazyLoadableIntegrations ) : Promise < IntegrationFn > {
34
34
const bundle = LazyLoadableIntegrations [ name ] ;
35
35
36
- if ( ! bundle || ! WindowWithMaybeIntegration . Sentry ) {
36
+ // `window.Sentry` is only set when using a CDN bundle, but this method can also be used via the NPM package
37
+ const sentryOnWindow = ( WindowWithMaybeIntegration . Sentry = WindowWithMaybeIntegration . Sentry || { } ) ;
38
+
39
+ if ( ! bundle ) {
37
40
throw new Error ( `Cannot lazy load integration: ${ name } ` ) ;
38
41
}
39
42
40
43
// Bail if the integration already exists
41
- const existing = WindowWithMaybeIntegration . Sentry [ name ] ;
44
+ const existing = sentryOnWindow [ name ] ;
42
45
if ( typeof existing === 'function' ) {
43
46
return existing ;
44
47
}
@@ -61,7 +64,7 @@ export async function lazyLoadIntegration(name: keyof typeof LazyLoadableIntegra
61
64
throw new Error ( `Error when loading integration: ${ name } ` ) ;
62
65
}
63
66
64
- const integrationFn = WindowWithMaybeIntegration . Sentry [ name ] ;
67
+ const integrationFn = sentryOnWindow [ name ] ;
65
68
66
69
if ( typeof integrationFn !== 'function' ) {
67
70
throw new Error ( `Could not load integration: ${ name } ` ) ;
Original file line number Diff line number Diff line change @@ -48,12 +48,6 @@ describe('lazyLoadIntegration', () => {
48
48
await expect ( ( ) => lazyLoadIntegration ( 'invalid!!!' ) ) . rejects . toThrow ( 'Cannot lazy load integration: invalid!!!' ) ;
49
49
} ) ;
50
50
51
- test ( 'it rejects without global Sentry variable' , async ( ) => {
52
- await expect ( ( ) => lazyLoadIntegration ( 'httpClientIntegration' ) ) . rejects . toThrow (
53
- 'Cannot lazy load integration: httpClientIntegration' ,
54
- ) ;
55
- } ) ;
56
-
57
51
test ( 'it does not inject a script tag if integration already exists' , async ( ) => {
58
52
// @ts -expect-error For testing sake
59
53
global . Sentry = Sentry ;
You can’t perform that action at this time.
0 commit comments