@@ -261,9 +261,9 @@ export class PlaywrightBrowserProvider implements BrowserProvider {
261261
262262 private createMocker ( ) : BrowserModuleMocker {
263263 const idPredicates = new Map < string , ( url : URL ) => boolean > ( )
264- const sessionIds = new Map < string , string [ ] > ( )
264+ const sessionIds = new Map < string , Set < string > > ( )
265265
266- function createPredicate ( sessionId : string , url : string ) {
266+ function createPredicate ( url : string ) {
267267 const moduleUrl = new URL ( url , 'http://localhost' )
268268 const predicate = ( url : URL ) => {
269269 if ( url . searchParams . has ( '_vitest_original' ) ) {
@@ -293,11 +293,7 @@ export class PlaywrightBrowserProvider implements BrowserProvider {
293293
294294 return true
295295 }
296- const ids = sessionIds . get ( sessionId ) || [ ]
297- ids . push ( moduleUrl . href )
298- sessionIds . set ( sessionId , ids )
299- idPredicates . set ( predicateKey ( sessionId , moduleUrl . href ) , predicate )
300- return predicate
296+ return { url : moduleUrl . href , predicate }
301297 }
302298
303299 function predicateKey ( sessionId : string , url : string ) {
@@ -307,7 +303,17 @@ export class PlaywrightBrowserProvider implements BrowserProvider {
307303 return {
308304 register : async ( sessionId : string , module : MockedModule ) : Promise < void > => {
309305 const page = this . getPage ( sessionId )
310- await page . context ( ) . route ( createPredicate ( sessionId , module . url ) , async ( route ) => {
306+ const { url : moduleUrl , predicate } = createPredicate ( module . url )
307+ const key = predicateKey ( sessionId , moduleUrl )
308+ const existingPredicate = idPredicates . get ( key )
309+ if ( existingPredicate ) {
310+ await page . context ( ) . unroute ( existingPredicate )
311+ }
312+ const ids = sessionIds . get ( sessionId ) ?? new Set < string > ( )
313+ ids . add ( moduleUrl )
314+ sessionIds . set ( sessionId , ids )
315+ idPredicates . set ( key , predicate )
316+ await page . context ( ) . route ( predicate , async ( route ) => {
311317 if ( module . type === 'manual' ) {
312318 const exports = Object . keys ( await module . resolve ( ) )
313319 const body = createManualModuleSource ( module . url , exports )
@@ -381,8 +387,8 @@ export class PlaywrightBrowserProvider implements BrowserProvider {
381387 } ,
382388 clear : async ( sessionId : string ) : Promise < void > => {
383389 const page = this . getPage ( sessionId )
384- const ids = sessionIds . get ( sessionId ) || [ ]
385- const promises = ids . map ( ( id ) => {
390+ const ids = sessionIds . get ( sessionId ) ?? new Set < string > ( )
391+ const promises = [ ... ids ] . map ( ( id ) => {
386392 const key = predicateKey ( sessionId , id )
387393 const predicate = idPredicates . get ( key )
388394 if ( predicate ) {
0 commit comments