test: more coverage for error handling
This commit is contained in:
@@ -6,10 +6,13 @@ import {
|
||||
nodeOps,
|
||||
watch,
|
||||
ref,
|
||||
nextTick
|
||||
nextTick,
|
||||
mockWarn
|
||||
} from '@vue/runtime-test'
|
||||
|
||||
describe('error handling', () => {
|
||||
mockWarn()
|
||||
|
||||
test('propagtaion', () => {
|
||||
const err = new Error('foo')
|
||||
const fn = jest.fn()
|
||||
@@ -86,6 +89,35 @@ describe('error handling', () => {
|
||||
expect(fn).toHaveBeenCalledWith(err, 'mounted hook', 'child')
|
||||
})
|
||||
|
||||
test('async error handling', async () => {
|
||||
const err = new Error('foo')
|
||||
const fn = jest.fn()
|
||||
|
||||
const Comp = {
|
||||
setup() {
|
||||
onErrorCaptured((err, instance, info) => {
|
||||
fn(err, info)
|
||||
return true
|
||||
})
|
||||
return () => h(Child)
|
||||
}
|
||||
}
|
||||
|
||||
const Child = {
|
||||
setup() {
|
||||
onMounted(async () => {
|
||||
throw err
|
||||
})
|
||||
},
|
||||
render() {}
|
||||
}
|
||||
|
||||
render(h(Comp), nodeOps.createElement('div'))
|
||||
expect(fn).not.toHaveBeenCalled()
|
||||
await new Promise(r => setTimeout(r))
|
||||
expect(fn).toHaveBeenCalledWith(err, 'mounted hook')
|
||||
})
|
||||
|
||||
test('error thrown in onErrorCaptured', () => {
|
||||
const err = new Error('foo')
|
||||
const err2 = new Error('bar')
|
||||
@@ -327,5 +359,36 @@ describe('error handling', () => {
|
||||
expect(fn).toHaveBeenCalledWith(err, 'component event handler')
|
||||
})
|
||||
|
||||
it('should warn unhandled', () => {
|
||||
const onError = jest.spyOn(console, 'error')
|
||||
onError.mockImplementation(() => {})
|
||||
const err = new Error('foo')
|
||||
const fn = jest.fn()
|
||||
|
||||
const Comp = {
|
||||
setup() {
|
||||
onErrorCaptured((err, instance, info) => {
|
||||
fn(err, info)
|
||||
})
|
||||
return () => h(Child)
|
||||
}
|
||||
}
|
||||
|
||||
const Child = {
|
||||
setup() {
|
||||
throw err
|
||||
},
|
||||
render() {}
|
||||
}
|
||||
|
||||
render(h(Comp), nodeOps.createElement('div'))
|
||||
expect(fn).toHaveBeenCalledWith(err, 'setup function')
|
||||
expect(
|
||||
`Unhandled error during execution of setup function`
|
||||
).toHaveBeenWarned()
|
||||
expect(onError).toHaveBeenCalledWith(err)
|
||||
onError.mockRestore()
|
||||
})
|
||||
|
||||
// native event handler handling should be tested in respective renderers
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user