@@ -264,6 +264,34 @@ describe('patchLayer', () => {
264264 checkSpans ( [ ] ) ;
265265 } ) ;
266266
267+ it ( 'pops storedLayers when ignoring router or request_handler type layers' , ( ) => {
268+ for ( const type of [ 'router' , 'request_handler' ] as const ) {
269+ const options : ExpressPatchLayerOptions = { ignoreLayersType : [ type ] } ;
270+ const req = Object . assign ( new EventEmitter ( ) , {
271+ originalUrl : '/a/b/c/layerPath' ,
272+ } ) as unknown as ExpressRequest ;
273+
274+ // simulate layers already stored for previous path segments
275+ storeLayer ( req , '/a' ) ;
276+ storeLayer ( req , '/b' ) ;
277+
278+ // patch a layer of the ignored type with a layerPath
279+ const layerHandleOriginal = vi . fn ( ) ;
280+ // layer.name must match what getLayerMetadata uses to classify each type:
281+ // 'router' → router, 'bound dispatch' → request_handler, other → middleware
282+ const layerName = type === 'router' ? 'router' : 'bound dispatch' ;
283+ const layer = { name : layerName , handle : layerHandleOriginal } as unknown as ExpressLayer ;
284+ patchLayer ( options , layer , '/c' ) ;
285+
286+ // storeLayer('/c') happens inside the patched handle, before being popped
287+ // after handle returns, storedLayers should be back to ['/a', '/b']
288+ layer . handle ( req , Object . assign ( new EventEmitter ( ) , { } ) as unknown as ExpressResponse ) ;
289+
290+ // the ignored layer's path must be cleaned up so subsequent layers see the correct route
291+ expect ( getStoredLayers ( req ) ) . toStrictEqual ( [ '/a' , '/b' ] ) ;
292+ }
293+ } ) ;
294+
267295 it ( 'warns about not setting name in default isolation scope' , async ( ) => {
268296 inDefaultIsolationScope = true ;
269297 DEBUG_BUILD = true ;
0 commit comments