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 #1760: do not console.error handled exceptions #1761

Merged
merged 1 commit into from Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions packages/test-utils/src/error.js
Expand Up @@ -22,13 +22,14 @@ function errorHandler(errorOrString, vm, info) {
vm._error = error
}

if (!instancedErrorHandlers.length) {
throw error
}
// should be one error handler, as only once can be registered with local vue
// regardless, if more exist (for whatever reason), invoke the other user defined error handlers
instancedErrorHandlers.forEach(instancedErrorHandler => {
instancedErrorHandler(error, vm, info)
})

throw error
}

export function throwIfInstancesThrew(vm) {
Expand Down
60 changes: 60 additions & 0 deletions test/specs/create-local-vue.spec.js
Expand Up @@ -151,6 +151,66 @@ describeWithShallowAndMount('createLocalVue', mountingMethod => {
}
)

itSkipIf(
vueVersion < 2.6,
'Exception suppresed in `errorHandler` is not logged to console.error',
async () => {
const component = Vue.component('TestComponent', {
template: '<button id="btn" @click="clickHandler">Click me</button>',
methods: {
clickHandler() {
throw new Error('Should not be logged')
}
}
})
const errorHandler = jest.fn()
const localVue = createLocalVue({
errorHandler
})
const wrapper = mountingMethod(component, { localVue })
await wrapper.vm.$nextTick()

const { error } = global.console
const spy = jest.spyOn(global.console, 'error')
await wrapper.trigger('click')
global.console.error = error
expect(spy).not.toHaveBeenCalled()
}
)

itSkipIf(
vueVersion < 2.6,
'Exception raised in `errorHandler` bubbles up',
async () => {
const component = Vue.component('TestComponent', {
template: '<button id="btn" @click="clickHandler">Click me</button>',
methods: {
clickHandler() {
throw new Error()
}
}
})
const errorHandler = (err, vm, info) => {
if (err) {
throw new Error('An error that should log')
}
}
const localVue = createLocalVue({
errorHandler
})
const wrapper = mountingMethod(component, { localVue })
await wrapper.vm.$nextTick()

const { error } = global.console
const spy = jest.spyOn(global.console, 'error')
await wrapper.trigger('click')
global.console.error = error
expect(spy).toHaveBeenCalledWith(
'[Vue warn]: Error in config.errorHandler: "Error: An error that should log"'
)
}
)

itSkipIf(
process.env.TEST_ENV === 'browser' || vueVersion < 2.6,
'Calls `errorHandler` when an error is thrown asynchronously',
Expand Down