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-core): handle error in async watchEffect #3129

Merged
merged 1 commit into from
Mar 23, 2021
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
49 changes: 48 additions & 1 deletion packages/runtime-core/__tests__/errorHandling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
ref,
nextTick,
defineComponent,
watchEffect
watchEffect,
createApp
} from '@vue/runtime-test'

describe('error handling', () => {
Expand Down Expand Up @@ -536,5 +537,51 @@ describe('error handling', () => {
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
})
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function doWatch(
if (cleanup) {
cleanup()
}
return callWithErrorHandling(
return callWithAsyncErrorHandling(
source,
instance,
ErrorCodes.WATCH_CALLBACK,
Expand Down