1+ // @ts -check
12const { get } = require ( 'http' ) ;
23const nock = require ( 'nock' ) ;
34const nodeSDK = require ( '@sentry/node' ) ;
@@ -132,14 +133,16 @@ function ensureWrappedGet(importedGet, url) {
132133 // As of Next 12.1, creating a `NextServer` instance (which we do immediately upon starting this test runner) loads
133134 // `_app`, which has the effect of initializing the SDK. So, unless something's gone wrong, we should always be able
134135 // to find the integration
135- let httpIntegration ;
136- try {
137- httpIntegration = nodeSDK . getCurrentHub ( ) . getClient ( ) . getIntegration ( nodeSDK . Integrations . Http ) ;
138- } catch ( err ) {
136+ const hub = nodeSDK . getCurrentHub ( ) ;
137+ const client = hub . getClient ( ) ;
138+
139+ if ( ! client ) {
139140 console . warn ( `Warning: Sentry SDK not set up at \`NextServer\` initialization. Request URL: ${ url } ` ) ;
140141 return importedGet ;
141142 }
142143
144+ const httpIntegration = client . getIntegration ( nodeSDK . Integrations . Http ) ;
145+
143146 // This rewraps `http.get` and `http.request`, which, at this point, look like `nockWrapper(sentryWrapper(get))` and
144147 // `nockWrapper(sentryWrapper(request))`. By the time we're done with this function, they'll look like
145148 // `sentryWrapper(nockWrapper(sentryWrapper(get)))` and `sentryWrapper(nockWrapper(sentryWrapper(request)))`,
@@ -160,7 +163,12 @@ function ensureWrappedGet(importedGet, url) {
160163 //
161164 // TODO: add in a "don't do this twice" check (in `fill`, maybe moved from `wrap`), so that we don't wrap the outer
162165 // wrapper with a third wrapper
163- httpIntegration . setupOnce ( ) ;
166+ if ( httpIntegration ) {
167+ httpIntegration . setupOnce (
168+ ( ) => undefined ,
169+ ( ) => hub ,
170+ ) ;
171+ }
164172
165173 // now that we've rewrapped it, grab the correct version of `get` for use in our tests
166174 const httpModule = require ( 'http' ) ;
0 commit comments