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): return hook() in wrappedHook to handle the async call in KeepAliveHook. #4978

Merged
merged 3 commits into from Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/runtime-core/__tests__/errorHandling.spec.ts
Expand Up @@ -9,6 +9,8 @@ import {
nextTick,
defineComponent,
watchEffect,
KeepAlive,
onActivated,
createApp
} from '@vue/runtime-test'

Expand Down Expand Up @@ -53,6 +55,7 @@ describe('error handling', () => {

test('propagation stoppage', () => {
ygj6 marked this conversation as resolved.
Show resolved Hide resolved
const err = new Error('foo')
const err2 = new Error('bar')
const fn = jest.fn()

const Comp = {
Expand All @@ -71,7 +74,7 @@ describe('error handling', () => {
fn(err, info, 'child')
return false
})
return () => h(GrandChild)
return () => h(KeepAlive, null, () => h(GrandChild))
}
}

Expand All @@ -80,17 +83,22 @@ describe('error handling', () => {
onMounted(() => {
throw err
})
onActivated(() => {
throw err2
})
return () => null
}
}

render(h(Comp), nodeOps.createElement('div'))
expect(fn).toHaveBeenCalledTimes(1)
expect(fn).toHaveBeenCalledTimes(2)
expect(fn).toHaveBeenCalledWith(err, 'mounted hook', 'child')
expect(fn).toHaveBeenCalledWith(err2, 'activated hook', 'child')
})

test('async error handling', async () => {
test.only('async error handling', async () => {
const err = new Error('foo')
const err2 = new Error('bar')
const fn = jest.fn()

const Comp = {
Expand All @@ -99,7 +107,7 @@ describe('error handling', () => {
fn(err, info)
return false
})
return () => h(Child)
return () => h(KeepAlive, null, () => h(Child))
}
}

Expand All @@ -108,14 +116,19 @@ describe('error handling', () => {
onMounted(async () => {
throw err
})
onActivated(async () => {
throw err2
})
},
render() {}
}

render(h(Comp), nodeOps.createElement('div'))
expect(fn).not.toHaveBeenCalled()
await new Promise(r => setTimeout(r))
expect(fn).toHaveBeenCalledTimes(2)
expect(fn).toHaveBeenCalledWith(err, 'mounted hook')
expect(fn).toHaveBeenCalledWith(err2, 'activated hook')
})

test('error thrown in onErrorCaptured', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-core/src/components/KeepAlive.ts
Expand Up @@ -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
Expand Down