cypress: `Cypress.on('uncaught:exception')` No Longer Preventing Tests From Failing after Upgrade to `10.0.2`

Current behavior

Cypress is not ignoring the following error: uncaughtexception

My cypress/support/e2e.js file is configured so that Cypress should return false on an uncaught:exception in order to prevent the test from failing. This is no longer working as expected after upgrading to the latest version, 10.0.2, as the test is failing.

Desired behavior

In this situation, Cypress should pass the it statement while ignoring the error and throwing any specified logging.

Test code to reproduce

Cypress.on('uncaught:exception', (err) => {
  // returning false here prevents Cypress from
  // failing the test
  console.log('Cypress detected uncaught exception: ', err);
  return false;
});

Cypress Version

10.0.2

Other

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 14
  • Comments: 24 (13 by maintainers)

Most upvoted comments

@danfooks since your error comes from the application itself, the problem is likely a bit different. I noticed you are using cy.origin, in which case you likely need a separate uncaught:exception handler in cy.origin to catch that error and not throw it in your main test, something like:

const baseUrl = 'https://www.sampleUrl.com/';

function testLoginPageLink(selector, pageDetails) {
  cy.get(selector)
    .should('have.attr', 'href').and('include', `${baseUrl}${pageDetails.path}`)
    .then((href) => {
      cy.origin(baseUrl,
        { args: { hrefPath: href.substring(26), title: pageDetails.title }}, ({ hrefPath, title }) => {
          Cypress.on('uncaught:exception', () => false)
          cy.visit(hrefPath);
          cy.contains(title);
        });
    });
}

@ZachJW34 For myself, it is occurring consistently for every test run. I did check the ResizeObserver bug ticket, which seems to be the root cause of my issue. Also, If I am correct I should not have to check for a regex expression to be present in the error as @willoliveira-air is doing, as I want to catch all errors, rather than just this specific one. If it helps you to reproduce, below is the code that is triggering the issue in my test. This occurs consistently (every test run) for both Chrome and Electron. The code uses an href from a button and cy.origin to navigate to an external page using the baseUrl and the path of the external page:

const baseUrl = 'https://www.sampleUrl.com/';

function testLoginPageLink(selector, pageDetails) {
  cy.get(selector)
    .should('have.attr', 'href').and('include', `${baseUrl}${pageDetails.path}`)
    .then((href) => {
      cy.origin(baseUrl,
        { args: { hrefPath: href.substring(26), title: pageDetails.title }}, ({ hrefPath, title }) => {
          cy.visit(hrefPath);
          cy.contains(title);
        });
    });
}

Thanks for picking this up Zach, and let me know if I can provide any further information!

@willoliveira-air I am going to continue our conversation on issue #22113 as I think you and @mlberkow are having the same, if not a very similar issue, i.e. a resize observer failure that is being generated from the test itself, not the application.

One thing I did notice that I found interesting is that it looks like the ResizeObserver failures in from @willoliveria-air 's case come from the test itself, which will not work with uncaught:exception, similar to #22113. What happens if you try the fail handler out of curiosity? Something like

    Cypress.on("fail", () => {
      return false;
    });