11import { expect , test , vi } from 'vitest'
22import { effectScope , nextTick , ref } from 'vue'
33
4- import { debug , setUidGenerator } from './debug'
4+ import {
5+ debug ,
6+ moduleScopes ,
7+ setUidGenerator ,
8+ } from './debug'
59import { byGroupId , byUid } from './registry'
610
711// ---- helpers ----------------------------------------------------------------
@@ -17,6 +21,7 @@ const setup = () => {
1721const teardown = ( ) => {
1822 byUid . clear ( )
1923 byGroupId . clear ( )
24+ moduleScopes . clear ( )
2025}
2126
2227// ---- debug() without options ------------------------------------------------
@@ -341,6 +346,47 @@ test('enable boolean false — never registers', async () => {
341346 teardown ( )
342347} )
343348
349+ // ---- module-level / HMR -----------------------------------------------------
350+
351+ test ( 'HMR: re-executing debug outside a scope with same groupId stops old scope and keeps only one entry' , async ( ) => {
352+ setup ( )
353+ const state = { count : 1 }
354+
355+ // First "module execution"
356+ debug ( 'myStore' , state )
357+ await nextTick ( )
358+ expect ( byUid . size ) . toBe ( 1 )
359+ expect ( moduleScopes . size ) . toBe ( 1 )
360+ const firstScope = moduleScopes . get ( 'myStore' )
361+
362+ // Simulate HMR: module re-executes, same groupId
363+ setup ( )
364+ const state2 = { count : 2 }
365+ debug ( 'myStore' , state2 )
366+ await nextTick ( )
367+
368+ // Old scope should be stopped, only one entry remains
369+ expect ( firstScope ?. active ) . toBe ( false )
370+ expect ( byUid . size ) . toBe ( 1 )
371+ expect ( moduleScopes . size ) . toBe ( 1 )
372+ expect ( moduleScopes . get ( 'myStore' ) ) . not . toBe ( firstScope )
373+
374+ teardown ( )
375+ } )
376+
377+ test ( 'module-level: registers entry outside a component scope' , async ( ) => {
378+ setup ( )
379+ const state = { count : 1 }
380+
381+ debug ( 'myStore' , state )
382+ await nextTick ( )
383+
384+ expect ( byUid . size ) . toBe ( 1 )
385+ expect ( moduleScopes . has ( 'myStore' ) ) . toBe ( true )
386+
387+ teardown ( )
388+ } )
389+
344390// ---- production guard -------------------------------------------------------
345391
346392test ( 'production guard: returns state but registers nothing when DEV is false' , async ( ) => {
0 commit comments