@@ -37,6 +37,7 @@ import {
37
37
toRef ,
38
38
triggerRef ,
39
39
} from '@vue/reactivity'
40
+ import { renderToString } from '@vue/server-renderer'
40
41
41
42
describe ( 'api: watch' , ( ) => {
42
43
it ( 'effect' , async ( ) => {
@@ -373,6 +374,43 @@ describe('api: watch', () => {
373
374
expect ( dummy ) . toBe ( 0 )
374
375
} )
375
376
377
+ it ( 'stopping the watcher (SSR)' , async ( ) => {
378
+ let dummy = 0
379
+ const count = ref < number > ( 1 )
380
+ const captureValue = ( value : number ) => {
381
+ dummy = value
382
+ }
383
+ const watchCallback = vi . fn ( newValue => {
384
+ captureValue ( newValue )
385
+ } )
386
+ const Comp = defineComponent ( {
387
+ created ( ) {
388
+ const getter = ( ) => this . count
389
+ captureValue ( getter ( ) ) // sets dummy to 1
390
+ const stop = this . $watch ( getter , watchCallback )
391
+ stop ( )
392
+ this . count = 2 // shouldn't trigger side effect
393
+ } ,
394
+ render ( ) {
395
+ return h ( 'div' , this . count )
396
+ } ,
397
+ setup ( ) {
398
+ return { count }
399
+ } ,
400
+ } )
401
+ let html
402
+ html = await renderToString ( h ( Comp ) )
403
+ // should not throw here
404
+ expect ( html ) . toBe ( `<div>2</div>` )
405
+ expect ( watchCallback ) . not . toHaveBeenCalled ( )
406
+ expect ( dummy ) . toBe ( 1 )
407
+ await nextTick ( )
408
+ count . value = 3 // shouldn't trigger side effect
409
+ await nextTick ( )
410
+ expect ( watchCallback ) . not . toHaveBeenCalled ( )
411
+ expect ( dummy ) . toBe ( 1 )
412
+ } )
413
+
376
414
it ( 'stopping the watcher (with source)' , async ( ) => {
377
415
const state = reactive ( { count : 0 } )
378
416
let dummy
0 commit comments