You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Define an errorHandler which suppresses a specific error type. The intended use case is to trigger some global side-effect whenever this type of exception is raised.
Write a test which triggers this exception via an event handler
// error.test.jsimport{createLocalVue,mount}from'@vue/test-utils';importVuefrom'vue';classMyCustomErrorextendsError{};constMyComponent=Vue.component('MyComponent',{template: '<button id="btn" @click="clickHandler">Click me</button>',methods: {clickHandler(){thrownewMyCustomError();}}});consterrorHandler=(err,vm,info)=>{if(errinstanceofMyCustomError){// TODO: Some side-effect like vm.$router.push, vm.$store.dispatchreturn;}throwerr// Raise others}describe('custom error handler',()=>{test('clicking the button does not raise MyCustomError',()=>{constlocalVue=createLocalVue({ errorHandler });constwrapper=mount(MyComponent,{ localVue });wrapper.trigger('click');});});
Expected behaviour
The test should pass
There should be no console.error
Actual behaviour
The test passes
The caught exception is thrown and then logged to console
Output:
PASS ./error.test.js
custom error handler
✓ clicking the button does not raise MyCustomError (53 ms)
console.error
[Vue warn]: Error in v-on handler: "Error"
found in
---> <MyComponent>
<Root>
at warn (node_modules/vue/dist/vue.runtime.common.dev.js:621:15)
at logError (node_modules/vue/dist/vue.runtime.common.dev.js:1880:5)
at globalHandleError (node_modules/vue/dist/vue.runtime.common.dev.js:1875:3)
at handleError (node_modules/vue/dist/vue.runtime.common.dev.js:1835:5)
at invokeWithErrorHandling (node_modules/vue/dist/vue.runtime.common.dev.js:1858:5)
at HTMLButtonElement.invoker (node_modules/vue/dist/vue.runtime.common.dev.js:2175:14)
at HTMLButtonElement.original._wrapper (node_modules/vue/dist/vue.runtime.common.dev.js:6895:25)
console.error
MyCustomError:
....
Possible Solution
Update the behaviour of vue-test-utils to match Vue's.
VueJS will log errors to console if:
No errorHandler is defined; or
An error was raised from within a custom errorHandler
Errors will not be logged if a defined errorHandler returns without throwing an exception.
functionglobalHandleError(err,vm,info){if(config.errorHandler){try{returnconfig.errorHandler.call(null,err,vm,info)}catch(e){// if the user intentionally throws the original error in the handler,// do not log it twiceif(e!==err){logError(e,null,'config.errorHandler')}}}logError(err,vm,info)}
The text was updated successfully, but these errors were encountered:
Subject of the issue
Exceptions that are caught and handled in an
errorHandler
are re-thrown and logged to console.error.vue-test-utils/packages/test-utils/src/error.js
Lines 27 to 31 in a821908
Steps to reproduce
Expected behaviour
Actual behaviour
Output:
Possible Solution
Update the behaviour of
vue-test-utils
to match Vue's.VueJS will log errors to console if:
errorHandler
is defined; orerrorHandler
Errors will not be logged if a defined
errorHandler
returns without throwing an exception.https://github.com/vuejs/vue/blob/b51430f598b354ed60851bb62885539bd25de3d8/src/core/util/error.js#L58-L71
The text was updated successfully, but these errors were encountered: