Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime): handle errors for computed watch keys (fix #11624) #11626

Merged
merged 1 commit into from
Aug 16, 2024
Merged
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
37 changes: 37 additions & 0 deletions packages/runtime-core/__tests__/errorHandling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,5 +670,42 @@ describe('error handling', () => {
)
})

// #11624
test('in computed that is used as key for watch', async () => {
const err = new Error('foo')
const fn = vi.fn()
const trigger = ref(false)

const Comp = {
setup() {
onErrorCaptured((err, instance, info) => {
fn(err, info)
return false
})
return () => h(Child)
},
}

const Child = {
setup() {
const foo = computed(() => {
if (trigger.value) throw err
return 1
})
watch(foo, () => {})
return () => null
},
}

render(h(Comp), nodeOps.createElement('div'))

trigger.value = true
await nextTick()
expect(fn).toHaveBeenCalledWith(
err,
ErrorTypeStrings[ErrorCodes.COMPONENT_UPDATE],
)
})

// native event handler handling should be tested in respective renderers
})
5 changes: 4 additions & 1 deletion packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,10 @@ function doWatch(
} else {
// default: 'pre'
job.flags! |= SchedulerJobFlags.PRE
if (instance) job.id = instance.uid
if (instance) {
job.id = instance.uid
job.i = instance
}
scheduler = () => queueJob(job)
}
effect.scheduler = scheduler
Expand Down