Skip to content

Vue catches errors while running Jest tests #9083

@chris-sorrells

Description

@chris-sorrells

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 the msg prop to cause Vue to generate an error.
  • Run the default example.spec.js test using npm 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.

bug


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".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions