]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): handle error in async KeepAlive hooks (#4978)
authorygj6 <7699524+ygj6@users.noreply.github.com>
Mon, 22 Nov 2021 07:50:19 +0000 (15:50 +0800)
committerGitHub <noreply@github.com>
Mon, 22 Nov 2021 07:50:19 +0000 (02:50 -0500)
packages/runtime-core/__tests__/components/KeepAlive.spec.ts
packages/runtime-core/src/components/KeepAlive.ts

index 4dce5d144cfdff8afa8d008013217614faab6151..b60931e1ffb1110e9c7df6c6b3b77d0f5a1f3bfa 100644 (file)
@@ -16,7 +16,9 @@ import {
   cloneVNode,
   provide,
   defineAsyncComponent,
-  Component
+  Component,
+  createApp,
+  onActivated
 } from '@vue/runtime-test'
 import { KeepAliveProps } from '../../src/components/KeepAlive'
 
@@ -874,4 +876,31 @@ describe('KeepAlive', () => {
     await nextTick()
     expect(serializeInner(root)).toBe('<p>1</p>')
   })
+
+  // #4976
+  test('handle error in async onActivated', async () => {
+    const err = new Error('foo')
+    const handler = jest.fn()
+
+    const app = createApp({
+      setup() {
+        return () => h(KeepAlive, null, () => h(Child))
+      }
+    })
+
+    const Child = {
+      setup() {
+        onActivated(async () => {
+          throw err
+        })
+      },
+      render() {}
+    }
+
+    app.config.errorHandler = handler
+    app.mount(nodeOps.createElement('div'))
+
+    await nextTick()
+    expect(handler).toHaveBeenCalledWith(err, {}, 'activated hook')
+  })
 })
index 30ad4400da6b17ab261b85b99eb5c80525492773..223a7c602f55d555b75d8065e7719e15b3b820f9 100644 (file)
@@ -381,7 +381,7 @@ function registerKeepAliveHook(
         }
         current = current.parent
       }
-      hook()
+      return hook()
     })
   injectHook(type, wrappedHook, target)
   // In addition to registering it on the target instance, we walk up the parent