@@ -52,8 +52,10 @@ vi.mock('../../../../src/debug-build', () => ({
5252 DEBUG_BUILD : true ,
5353} ) ) ;
5454const debugErrors : [ string , Error ] [ ] = [ ] ;
55+ const debugWarnings : string [ ] = [ ] ;
5556vi . mock ( '../../../../src/utils/debug-logger' , ( ) => ( {
5657 debug : {
58+ warn : ( msg : string ) => debugWarnings . push ( msg ) ,
5759 error : ( msg : string , er : Error ) => {
5860 debugErrors . push ( [ msg , er ] ) ;
5961 } ,
@@ -129,43 +131,58 @@ function getExpress5(): ExpressExportv5 & { spies: ExpressSpies } {
129131}
130132
131133describe ( 'patchExpressModule' , ( ) => {
132- it ( 'throws trying to patch/unpatch the wrong thing' , ( ) => {
134+ it ( 'throws trying to patch the wrong thing' , ( ) => {
133135 expect ( ( ) => {
134136 patchExpressModule ( { } as unknown as ExpressModuleExport , ( ) => ( { } ) ) ;
135137 } ) . toThrowError ( 'no valid Express route function to instrument' ) ;
136138 } ) ;
137139
138- it ( 'can patch and restore expressv4 style module' , ( ) => {
140+ it ( 'throws trying to patch without a getOptions getter' , ( ) => {
141+ const express = getExpress4 ( ) ;
142+ expect ( ( ) => {
143+ //@ts -expect-error The type error prevents this, by design
144+ patchExpressModule ( express ) ;
145+ } ) . toThrowError ( '`patchExpressModule(moduleExports, getOptions)` requires a `getOptions` callback' ) ;
146+ } ) ;
147+
148+ it ( 'can patch expressv4 style module' , ( ) => {
139149 for ( const useDefault of [ false , true ] ) {
140150 const express = getExpress4 ( ) ;
141151 const moduleExports = useDefault ? { default : express } : express ;
142152 const r = express . Router as ExpressRouterv4 ;
143153 const a = express . application ;
144- const getOptions = ( ) => ( { } ) ;
145154 expect ( ( r . use as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
146155 expect ( ( r . route as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
147156 expect ( ( a . use as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
148157
149- patchExpressModule ( moduleExports , getOptions ) ;
158+ patchExpressModule ( { express : moduleExports } ) ;
159+ expect ( debugWarnings ) . toStrictEqual ( [
160+ '[Express] `patchExpressModule(options)` is deprecated. Use `patchExpressModule(moduleExports, getOptions)` instead.' ,
161+ ] ) ;
150162
151163 expect ( typeof ( r . use as WrappedFunction ) . __sentry_original__ ) . toBe ( 'function' ) ;
152164 expect ( typeof ( r . route as WrappedFunction ) . __sentry_original__ ) . toBe ( 'function' ) ;
153165 expect ( typeof ( a . use as WrappedFunction ) . __sentry_original__ ) . toBe ( 'function' ) ;
154166 }
155167 } ) ;
156168
157- it ( 'can patch and restore expressv5 style module' , ( ) => {
169+ it ( 'can patch expressv5 style module' , ( ) => {
158170 for ( const useDefault of [ false , true ] ) {
159171 const express = getExpress5 ( ) ;
160172 const r = express . Router as ExpressRouterv5 ;
161173 const a = express . application ;
162174 const moduleExports = useDefault ? { default : express } : express ;
163- const getOptions = ( ) => ( { } ) ;
164175 expect ( ( r . prototype . use as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
165176 expect ( ( r . prototype . route as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
166177 expect ( ( a . use as WrappedFunction ) . __sentry_original__ ) . toBe ( undefined ) ;
167178
168- patchExpressModule ( moduleExports , getOptions ) ;
179+ // verify that the debug warning doesn't fire a second time
180+ // vitest doesn't guarantee test ordering, so just verify
181+ // in both places that there's only one warning.
182+ patchExpressModule ( { express : moduleExports } ) ;
183+ expect ( debugWarnings ) . toStrictEqual ( [
184+ '[Express] `patchExpressModule(options)` is deprecated. Use `patchExpressModule(moduleExports, getOptions)` instead.' ,
185+ ] ) ;
169186
170187 expect ( typeof ( r . prototype . use as WrappedFunction ) . __sentry_original__ ) . toBe ( 'function' ) ;
171188 expect ( typeof ( r . prototype . route as WrappedFunction ) . __sentry_original__ ) . toBe ( 'function' ) ;
0 commit comments