test: add failing test cases for onScopeDispose inside watchers

This commit is contained in:
Fernando Fernández 2025-01-11 19:24:40 +00:00 committed by GitHub
parent 22dcbf3e20
commit bf329cb35c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import {
type WatchOptions,
type WatchScheduler,
computed,
onScopeDispose,
onWatcherCleanup,
ref,
watch,
@ -277,4 +278,24 @@ describe('watch', () => {
expect(dummy).toEqual([1, 2, 3])
})
// #12681
test('onScopeDispose inside non-immediate watcher', () => {
const cleanupSpy = vi.fn()
const cbSpy = vi.fn(() => {
onScopeDispose(cleanupSpy)
})
const scope = new EffectScope()
scope.run(() => {
const signal = ref(false)
watch(signal, cbSpy)
signal.value = true
})
scope.stop()
expect(cbSpy).toBeCalledTimes(1)
expect(cleanupSpy).toBeCalledTimes(1)
})
})

View File

@ -2010,4 +2010,24 @@ describe('api: watch', () => {
createApp(App).mount(root)
expect(onCleanup).toBeCalledTimes(0)
})
// #12681
test('onScopeDispose inside non-immediate watcher that ran', () => {
const cleanupSpy = vi.fn()
const cbSpy = vi.fn(() => {
onScopeDispose(cleanupSpy)
})
const scope = effectScope()
scope.run(() => {
const signal = ref(false)
watch(signal, cbSpy)
signal.value = true
})
scope.stop()
expect(cbSpy).toBeCalledTimes(1)
expect(cleanupSpy).toBeCalledTimes(1)
})
})