-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
Description
Version
2.5.17
Reproduction link
https://github.com/chris-sorrells/vue-jest-console-error-bug
Relevant code added to scaffolding
Steps to reproduce
- Scaffold a new Vue project using
vue-cli
. Use Jest for unit testing. - Create a
mounted()
lifecycle hook in the HelloWorld.vue component. - Within
mounted()
, directly manipulate themsg
prop to cause Vue to generate an error. - Run the default
example.spec.js
test usingnpm run test:unit
.
What is expected?
The test should fail due to the error generated when directly manipulating the msg
prop.
What is actually happening?
Vue catches the error and uses console.error
to display it. Because console.error
does not fail Jest tests, the test pass despite the error thrown.
The underlying problem is that Vue is trying to determine whether it is appropriate to catch and console.error
an error or allow the error to remain uncaught based on if Vue believes it is in a browser. Unfortunately, because Jest emulates the browser environment, error handling does not function as intended and tests will succeed inappropriately.
A fix for this would be to also check whether Vue is in a Node environment before catching the error. This would allow Jest tests to operate as intended, and maintain the behavior of error handling in the browser.
Another fix would be to re-throw the error if process.env.NODE_ENV === "test"
.