]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): handle error in async watchEffect (#3129)
authoredison <daiwei521@126.com>
Tue, 23 Mar 2021 09:20:52 +0000 (17:20 +0800)
committerGitHub <noreply@github.com>
Tue, 23 Mar 2021 09:20:52 +0000 (10:20 +0100)
packages/runtime-core/__tests__/errorHandling.spec.ts
packages/runtime-core/src/apiWatch.ts

index 36e3f6fcd6c2bfae6aca211869633d914e7fe8e4..10d28e441eab048951d1ce0b7b2d53d71d0be3ba 100644 (file)
@@ -8,7 +8,8 @@ import {
   ref,
   nextTick,
   defineComponent,
-  watchEffect
+  watchEffect,
+  createApp
 } from '@vue/runtime-test'
 
 describe('error handling', () => {
@@ -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
 })
index 0e0a549c8a9082e156c8b05ed9f04dfdc596f449..85b3fff979290402a1ac903c85d216f8e9db9785 100644 (file)
@@ -204,7 +204,7 @@ function doWatch(
         if (cleanup) {
           cleanup()
         }
-        return callWithErrorHandling(
+        return callWithAsyncErrorHandling(
           source,
           instance,
           ErrorCodes.WATCH_CALLBACK,