Skip to content

Commit 8bcaad4

Browse files
authored
fix(watch): handle errors in computed used as watch source (#11626)
close #11624
1 parent c42919a commit 8bcaad4

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/runtime-core/__tests__/errorHandling.spec.ts

+37
Original file line numberDiff line numberDiff line change
@@ -670,5 +670,42 @@ describe('error handling', () => {
670670
)
671671
})
672672

673+
// #11624
674+
test('in computed that is used as key for watch', async () => {
675+
const err = new Error('foo')
676+
const fn = vi.fn()
677+
const trigger = ref(false)
678+
679+
const Comp = {
680+
setup() {
681+
onErrorCaptured((err, instance, info) => {
682+
fn(err, info)
683+
return false
684+
})
685+
return () => h(Child)
686+
},
687+
}
688+
689+
const Child = {
690+
setup() {
691+
const foo = computed(() => {
692+
if (trigger.value) throw err
693+
return 1
694+
})
695+
watch(foo, () => {})
696+
return () => null
697+
},
698+
}
699+
700+
render(h(Comp), nodeOps.createElement('div'))
701+
702+
trigger.value = true
703+
await nextTick()
704+
expect(fn).toHaveBeenCalledWith(
705+
err,
706+
ErrorTypeStrings[ErrorCodes.COMPONENT_UPDATE],
707+
)
708+
})
709+
673710
// native event handler handling should be tested in respective renderers
674711
})

packages/runtime-core/src/apiWatch.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,10 @@ function doWatch(
400400
} else {
401401
// default: 'pre'
402402
job.flags! |= SchedulerJobFlags.PRE
403-
if (instance) job.id = instance.uid
403+
if (instance) {
404+
job.id = instance.uid
405+
job.i = instance
406+
}
404407
scheduler = () => queueJob(job)
405408
}
406409
effect.scheduler = scheduler

0 commit comments

Comments
 (0)