Skip to content

Commit

Permalink
test: add failing test cases for onScopeDispose inside watchers
Browse files Browse the repository at this point in the history
  • Loading branch information
ferferga authored Jan 11, 2025
1 parent 22dcbf3 commit bf329cb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/reactivity/__tests__/watch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type WatchOptions,
type WatchScheduler,
computed,
onScopeDispose,
onWatcherCleanup,
ref,
watch,
Expand Down Expand Up @@ -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)
})
})
20 changes: 20 additions & 0 deletions packages/runtime-core/__tests__/apiWatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 2030 in packages/runtime-core/__tests__/apiWatch.spec.ts

View workflow job for this annotation

GitHub Actions / test / unit-test

packages/runtime-core/__tests__/apiWatch.spec.ts > api: watch > onScopeDispose inside non-immediate watcher that ran

AssertionError: expected "spy" to be called 1 times, but got 0 times ❯ packages/runtime-core/__tests__/apiWatch.spec.ts:2030:19
expect(cleanupSpy).toBeCalledTimes(1)
})
})

0 comments on commit bf329cb

Please sign in to comment.