]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(watch): handle errors in computed used as watch source (#11626)
authorJakob <jak-ch-ll@mailbox.org>
Fri, 16 Aug 2024 08:14:08 +0000 (10:14 +0200)
committerGitHub <noreply@github.com>
Fri, 16 Aug 2024 08:14:08 +0000 (16:14 +0800)
close #11624

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

index 4cf4ffe31268b9b4cbfc02f052e19d377b694122..0cd3efa588c410989ace91a9dbfdb1d11ecac0f4 100644 (file)
@@ -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
 })
index 479a263b39bf9cce53a7fb5d908f802fefc22097..35b488052f9f7a4eecfbc72b0c5ad7f55825c049 100644 (file)
@@ -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