@@ -61,12 +61,12 @@ vi.mock('../../../../src/utils/debug-logger', () => ({
6161} ) ) ;
6262
6363beforeEach ( ( ) => ( patchLayerCalls . length = 0 ) ) ;
64- const patchLayerCalls : [ options : ExpressIntegrationOptions , layer : ExpressLayer , layerPath ?: string ] [ ] = [ ] ;
64+ const patchLayerCalls : [ getOptions : ( ) => ExpressIntegrationOptions , layer : ExpressLayer , layerPath ?: string ] [ ] = [ ] ;
6565
6666vi . mock ( '../../../../src/integrations/express/patch-layer' , ( ) => ( {
67- patchLayer : ( options : ExpressIntegrationOptions , layer ?: ExpressLayer , layerPath ?: string ) => {
67+ patchLayer : ( getOptions : ( ) => ExpressIntegrationOptions , layer ?: ExpressLayer , layerPath ?: string ) => {
6868 if ( layer ) {
69- patchLayerCalls . push ( [ options , layer , layerPath ] ) ;
69+ patchLayerCalls . push ( [ getOptions , layer , layerPath ] ) ;
7070 }
7171 } ,
7272} ) ) ;
@@ -131,24 +131,22 @@ function getExpress5(): ExpressExportv5 & { spies: ExpressSpies } {
131131describe ( 'patchExpressModule' , ( ) => {
132132 it ( 'throws trying to patch/unpatch the wrong thing' , ( ) => {
133133 expect ( ( ) => {
134- patchExpressModule ( {
135- express : { } as unknown as ExpressModuleExport ,
136- } as unknown as ExpressIntegrationOptions ) ;
134+ patchExpressModule ( { } as unknown as ExpressModuleExport , ( ) => ( { } ) ) ;
137135 } ) . toThrowError ( 'no valid Express route function to instrument' ) ;
138136 } ) ;
139137
140138 it ( 'can patch and restore expressv4 style module' , ( ) => {
141139 for ( const useDefault of [ false , true ] ) {
142140 const express = getExpress4 ( ) ;
143- const module = useDefault ? { default : express } : express ;
141+ const moduleExports = useDefault ? { default : express } : express ;
144142 const r = express . Router as ExpressRouterv4 ;
145143 const a = express . application ;
146- const options = { express : module } as unknown as ExpressIntegrationOptions ;
144+ const getOptions = ( ) => ( { } ) ;
147145 expect ( ( r . use as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
148146 expect ( ( r . route as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
149147 expect ( ( a . use as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
150148
151- patchExpressModule ( options ) ;
149+ patchExpressModule ( moduleExports , getOptions ) ;
152150
153151 expect ( typeof ( r . use as WrappedFunction ) . __sentry_original__ ) . toBe ( 'function' ) ;
154152 expect ( typeof ( r . route as WrappedFunction ) . __sentry_original__ ) . toBe ( 'function' ) ;
@@ -161,13 +159,13 @@ describe('patchExpressModule', () => {
161159 const express = getExpress5 ( ) ;
162160 const r = express . Router as ExpressRouterv5 ;
163161 const a = express . application ;
164- const module = useDefault ? { default : express } : express ;
165- const options = { express : module } as unknown as ExpressIntegrationOptions ;
162+ const moduleExports = useDefault ? { default : express } : express ;
163+ const getOptions = ( ) => ( { } ) ;
166164 expect ( ( r . prototype . use as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
167165 expect ( ( r . prototype . route as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
168166 expect ( ( a . use as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
169167
170- patchExpressModule ( options ) ;
168+ patchExpressModule ( moduleExports , getOptions ) ;
171169
172170 expect ( typeof ( r . prototype . use as WrappedFunction ) . __sentry_original__ ) . toBe ( 'function' ) ;
173171 expect ( typeof ( r . prototype . route as WrappedFunction ) . __sentry_original__ ) . toBe ( 'function' ) ;
@@ -178,27 +176,27 @@ describe('patchExpressModule', () => {
178176 it ( 'calls patched and original Router.route' , ( ) => {
179177 const expressv4 = getExpress4 ( ) ;
180178 const { spies } = expressv4 ;
181- const options = { express : expressv4 } ;
182- patchExpressModule ( options ) ;
179+ const getOptions = ( ) => ( { } ) ;
180+ patchExpressModule ( expressv4 , getOptions ) ;
183181 expressv4 . Router . route ( 'a' ) ;
184182 expect ( spies . routerRoute ) . toHaveBeenCalledExactlyOnceWith ( 'a' ) ;
185183 } ) ;
186184
187185 it ( 'calls patched and original Router.use' , ( ) => {
188186 const expressv4 = getExpress4 ( ) ;
189187 const { spies } = expressv4 ;
190- const options = { express : expressv4 } ;
191- patchExpressModule ( options ) ;
188+ const getOptions = ( ) => ( { } ) ;
189+ patchExpressModule ( expressv4 , getOptions ) ;
192190 expressv4 . Router . use ( 'a' ) ;
193- expect ( patchLayerCalls ) . toStrictEqual ( [ [ options , { name : 'layerFinal' } , 'a' ] ] ) ;
191+ expect ( patchLayerCalls ) . toStrictEqual ( [ [ getOptions , { name : 'layerFinal' } , 'a' ] ] ) ;
194192 expect ( spies . routerUse ) . toHaveBeenCalledExactlyOnceWith ( 'a' ) ;
195193 } ) ;
196194
197195 it ( 'skips patchLayer call in Router.use if no layer in the stack' , ( ) => {
198196 const expressv4 = getExpress4 ( ) ;
199197 const { spies } = expressv4 ;
200- const options = { express : expressv4 } ;
201- patchExpressModule ( options ) ;
198+ const getOptions = ( ) => ( { } ) ;
199+ patchExpressModule ( expressv4 , getOptions ) ;
202200 const { stack } = expressv4 . Router ;
203201 expressv4 . Router . stack = [ ] ;
204202 expressv4 . Router . use ( 'a' ) ;
@@ -210,28 +208,28 @@ describe('patchExpressModule', () => {
210208 it ( 'calls patched and original application.use' , ( ) => {
211209 const expressv4 = getExpress4 ( ) ;
212210 const { spies } = expressv4 ;
213- const options = { express : expressv4 } ;
214- patchExpressModule ( options ) ;
211+ const getOptions = ( ) => ( { } ) ;
212+ patchExpressModule ( expressv4 , getOptions ) ;
215213 expressv4 . application . use ( 'a' ) ;
216- expect ( patchLayerCalls ) . toStrictEqual ( [ [ options , { name : 'layerFinal' } , 'a' ] ] ) ;
214+ expect ( patchLayerCalls ) . toStrictEqual ( [ [ getOptions , { name : 'layerFinal' } , 'a' ] ] ) ;
217215 expect ( spies . appUse ) . toHaveBeenCalledExactlyOnceWith ( 'a' ) ;
218216 } ) ;
219217
220218 it ( 'calls patched and original application.use on express v5' , ( ) => {
221219 const expressv5 = getExpress5 ( ) ;
222220 const { spies } = expressv5 ;
223- const options = { express : expressv5 } ;
224- patchExpressModule ( options ) ;
221+ const getOptions = ( ) => ( { } ) ;
222+ patchExpressModule ( expressv5 , getOptions ) ;
225223 expressv5 . application . use ( 'a' ) ;
226- expect ( patchLayerCalls ) . toStrictEqual ( [ [ options , { name : 'layerFinal' } , 'a' ] ] ) ;
224+ expect ( patchLayerCalls ) . toStrictEqual ( [ [ getOptions , { name : 'layerFinal' } , 'a' ] ] ) ;
227225 expect ( spies . appUse ) . toHaveBeenCalledExactlyOnceWith ( 'a' ) ;
228226 } ) ;
229227
230228 it ( 'skips patchLayer on application.use if no router found' , ( ) => {
231229 const expressv4 = getExpress4 ( ) ;
232230 const { spies } = expressv4 ;
233- const options = { express : expressv4 } ;
234- patchExpressModule ( options ) ;
231+ const getOptions = ( ) => ( { } ) ;
232+ patchExpressModule ( expressv4 , getOptions ) ;
235233 const app = expressv4 . application as {
236234 _router ?: ExpressRoute ;
237235 } ;
@@ -246,8 +244,9 @@ describe('patchExpressModule', () => {
246244
247245 it ( 'debug error when patching fails' , ( ) => {
248246 const expressv5 = getExpress5 ( ) ;
249- patchExpressModule ( { express : expressv5 } ) ;
250- patchExpressModule ( { express : expressv5 } ) ;
247+ const getOptions = ( ) => ( { } ) ;
248+ patchExpressModule ( expressv5 , getOptions ) ;
249+ patchExpressModule ( expressv5 , getOptions ) ;
251250 expect ( debugErrors ) . toStrictEqual ( [
252251 [ 'Failed to patch express route method:' , new Error ( 'Attempting to wrap method route multiple times' ) ] ,
253252 [ 'Failed to patch express use method:' , new Error ( 'Attempting to wrap method use multiple times' ) ] ,
0 commit comments