Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/reactivity/__tests__/watch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,47 @@ describe('watch', () => {
])
})

test('call option with async error handling', async () => {
const onError = vi.fn()
const call: WatchOptions['call'] = function call(fn, type, args) {
if (Array.isArray(fn)) {
fn.forEach(f => call(f, type, args))
return
}
fn.apply(null, args).catch((error: unknown) => {
onError(error)
})
}

const source1 = ref(0)
watch(
source1,
async () => {
throw 'oops in watch'
},
{ call },
)

source1.value++
await nextTick()
expect(onError.mock.calls.length).toBe(1)
expect(onError.mock.calls[0]).toMatchObject(['oops in watch'])

const source2 = ref(0)
watch(
source2,
async () => {
throw 'oops in once watch'
},
{ call, once: true },
)

source2.value++
await nextTick()
expect(onError.mock.calls.length).toBe(2)
expect(onError.mock.calls[1]).toMatchObject(['oops in once watch'])
})

test('watch with onWatcherCleanup', async () => {
let dummy = 0
let source: Ref<number>
Expand Down
3 changes: 2 additions & 1 deletion packages/reactivity/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,9 @@ export function watch(
if (once && cb) {
const _cb = cb
cb = (...args) => {
_cb(...args)
const res = _cb(...args)
watchHandle()
return res
}
}

Expand Down