cypress: Cypress Doesn't Fail Test on Range of Unhandled Exceptions

Current behavior:

Cypress passes test, although the HTML page contains a script in the head, here’s the full markup:

<html lang="en">
  <head>
    <title>Hello World</title>
    <script>
      (async function () {
        window.onerror = (_a, _b, _c, _d, err) => {
          throw new Error('new err from within onerror');
          throw err;
        };
        setTimeout(() => { throw new Error('original error'); });
        throw new Error('regular promise rejection');
      })()
    </script>
  </head>
  <body>
    <div>
      <h1>This should fail...</h1>
    </div>
  </body>
</html>

Screenshot

screen shot 2019-01-15 at 11 34 54 am

Desired behavior:

Cypress should fail the test if there’s:

  • An unhandled promise rejection
  • A re-thrown error from within window.onerror
  • A new error from within window.onerror

Steps to reproduce:

Setup Cypress in a new repo, serve a static Index.html file with a script in the head like the example I’ve provided. Run the spec and watch it pass…

Versions

Cypress 3.1.4 Electron 59 macOS 10.14.2

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 2
  • Comments: 30 (14 by maintainers)

Most upvoted comments

Is there any way to fail a test based on console errors?

Was able to reproduce. Added this test to cypress-example-kitchensink app:

it('should fail', () => {
  cy.visit('app/index.html')
})

Add this to index.html:

  <script>
    (async function () {
      const x = 1;
      throw new Error('error!');
    })()
  </script>

I have tried it and confirmed the test finishes too quickly - the error is thrown after the test. We have described this in the blog post https://www.cypress.io/blog/2020/01/16/when-can-the-test-stop/

If you suspect the page throws an error, the simplest way is to add a wait at the end

/// <reference types="cypress" />
it('should fail', () => {
  cy.visit('index.html').wait(100)
})

which fails the test correctly.

Screen Shot 2020-02-20 at 2 01 54 PM

@jennifer-shehane is there anything else we can do?

Running 4.0.1 and unhandled rejections are still passing.

any news on this?

I am having the same issue when errors occur inside of a Vue event handler, the error is logged on the console but the test does not fail. Is there any way to Fail a running test from application code?

Hey @andezzat, I have an update on this issue.

Currently, Chrome is the only browser that throws unhandledrejection events instead of error events. Cypress only listens to error events to fail the test. However, in 4.0, we’ve extended Cypress to also listen to unhandledrejection events, so be on the lookout for 4.0 release. https://github.com/cypress-io/cypress/issues/2884