fix(runtime-core): handle error in async watchEffect (#3129)

This commit is contained in:
edison 2021-03-23 17:20:52 +08:00 committed by GitHub
parent 21d1288133
commit eb1fae63f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 2 deletions

View File

@ -8,7 +8,8 @@ import {
ref, ref,
nextTick, nextTick,
defineComponent, defineComponent,
watchEffect watchEffect,
createApp
} from '@vue/runtime-test' } from '@vue/runtime-test'
describe('error handling', () => { describe('error handling', () => {
@ -536,5 +537,51 @@ describe('error handling', () => {
log.mockRestore() log.mockRestore()
}) })
//# 3127
test('handle error in watch & watchEffect', async () => {
const error1 = new Error('error1')
const error2 = new Error('error2')
const error3 = new Error('error3')
const error4 = new Error('error4')
const handler = jest.fn()
const app = createApp({
setup() {
const count = ref(1)
watch(
count,
() => {
throw error1
},
{ immediate: true }
)
watch(
count,
async () => {
throw error2
},
{ immediate: true }
)
watchEffect(() => {
throw error3
})
watchEffect(async () => {
throw error4
})
},
render() {}
})
app.config.errorHandler = handler
app.mount(nodeOps.createElement('div'))
await nextTick()
expect(handler).toHaveBeenCalledWith(error1, {}, 'watcher callback')
expect(handler).toHaveBeenCalledWith(error2, {}, 'watcher callback')
expect(handler).toHaveBeenCalledWith(error3, {}, 'watcher callback')
expect(handler).toHaveBeenCalledWith(error4, {}, 'watcher callback')
expect(handler).toHaveBeenCalledTimes(4)
})
// native event handler handling should be tested in respective renderers // native event handler handling should be tested in respective renderers
}) })

View File

@ -204,7 +204,7 @@ function doWatch(
if (cleanup) { if (cleanup) {
cleanup() cleanup()
} }
return callWithErrorHandling( return callWithAsyncErrorHandling(
source, source,
instance, instance,
ErrorCodes.WATCH_CALLBACK, ErrorCodes.WATCH_CALLBACK,