Skip to content

Commit 820a143

Browse files
authored
fix(runtime-core): handle error in async KeepAlive hooks (#4978)
1 parent 6172023 commit 820a143

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

packages/runtime-core/__tests__/components/KeepAlive.spec.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import {
1616
cloneVNode,
1717
provide,
1818
defineAsyncComponent,
19-
Component
19+
Component,
20+
createApp,
21+
onActivated
2022
} from '@vue/runtime-test'
2123
import { KeepAliveProps } from '../../src/components/KeepAlive'
2224

@@ -874,4 +876,31 @@ describe('KeepAlive', () => {
874876
await nextTick()
875877
expect(serializeInner(root)).toBe('<p>1</p>')
876878
})
879+
880+
// #4976
881+
test('handle error in async onActivated', async () => {
882+
const err = new Error('foo')
883+
const handler = jest.fn()
884+
885+
const app = createApp({
886+
setup() {
887+
return () => h(KeepAlive, null, () => h(Child))
888+
}
889+
})
890+
891+
const Child = {
892+
setup() {
893+
onActivated(async () => {
894+
throw err
895+
})
896+
},
897+
render() {}
898+
}
899+
900+
app.config.errorHandler = handler
901+
app.mount(nodeOps.createElement('div'))
902+
903+
await nextTick()
904+
expect(handler).toHaveBeenCalledWith(err, {}, 'activated hook')
905+
})
877906
})

packages/runtime-core/src/components/KeepAlive.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ function registerKeepAliveHook(
381381
}
382382
current = current.parent
383383
}
384-
hook()
384+
return hook()
385385
})
386386
injectHook(type, wrappedHook, target)
387387
// In addition to registering it on the target instance, we walk up the parent

0 commit comments

Comments
 (0)