cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.78k stars 3.17k forks source link

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

Closed danfooks closed 1 year ago

danfooks commented 2 years ago

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

mjhenkes commented 2 years ago

Possibly related to #22113

ZachJW34 commented 2 years ago

@danfooks I'm not able to reproduce this issue on Cypress v10.0.2. Is this error specific to ResizeObserver? Have you checked out the issue @mjhenkes linked to see if it is an issue with how you are matching the resize observer error text?

If it is a more general issue and Cypress.on('uncaught:exception', (err) => {}) is not working for you, can you provide a minimal reproduction?

willoliveira-air commented 2 years ago

@ZachJW34 I'm seeing the same problem from @danfooks within the ResizeObserver and the pattern seems to be fine because it is working for most part of the scenarios.

const resizeObserverLoopErrRe = /^[^(ResizeObserver loop limit exceeded)]/;
Cypress.on('uncaught:exception', (err) => {
  /* returning false here prevents Cypress from failing the test */
  if (resizeObserverLoopErrRe.test(err.message)) {
    return false;
  }
});

It is not reproduced all the time in Electron or Chrome (I didn't check in Mozilla yet). It is happening every few test runs (the same test but not all the runs).

The following error originated from your test code, not from Cypress.

  > ResizeObserver loop limit exceeded

When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.

View stack trace
at <unknown> (http://localhost:3000/__/#/specs/runner?file=cypress/e2e/error-states.spec.ts:0:0)

Please let me know if you need more details. Thanks.

danfooks commented 2 years ago

@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!

AtofStryker commented 2 years ago

Hey @danfooks & @willoliveira-air. Are either of you able to produce a full reproducible example? I am trying to reproduce this, but am struggling a bit.

AtofStryker commented 2 years ago

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;
    });
AtofStryker commented 2 years ago

@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);
        });
    });
}
willoliveira-air commented 2 years ago

@AtofStryker thanks for taking this one.

Well, thanks for adding this note about the debug approach. When I'm adding your suggestion on error instead of the uncaught:exception. Sometimes I'm able to see the error from my environment itself and sometimes I can't because it is running fine.

So, on the same way from the resizeObserver error, it is intermittent. Screenshot 2022-06-09 at 13 21 29

Another point is regarding the browser. I was not able to reproduce in Chrome or Firefox. Only in Electron v100 if that helps.

Please let me know if you need more details and I can provide them. Thanks.

danfooks commented 2 years ago

@AtofStryker Thank you for this recommendation. This solution seems to work! This is actually my first time using cy.origin, so I was unaware that we had to catch exceptions separately rather than rely on the exception handler in e2e.js. After bumping to 10.0.2, this is the only place in our tests where this ResizeObserver error was occurring, and the only place we are using cy.origin, so naturally it makes sense they could be related.

AtofStryker commented 2 years ago

handler in e2e.js. After bumping to 10.0.2, this is the only place in our tests where this ResizeObserver error was

@danfooks I'm glad that solution is working for you! I was looking through the cy.origin docs myself and couldn't find a clear area where this kind of event behavior is described, so I am following up with our developer experience team to verify a location for this type of thing. Are you running into any additional issues or do you feel this issue might be ready to close?

AtofStryker commented 2 years ago

@AtofStryker thanks for taking this one.

Well, thanks for adding this note about the debug approach. When I'm adding your suggestion on error instead of the uncaught:exception. Sometimes I'm able to see the error from my environment itself and sometimes I can't because it is running fine.

So, on the same way from the resizeObserver error, it is intermittent. Screenshot 2022-06-09 at 13 21 29

Another point is regarding the browser. I was not able to reproduce in Chrome or Firefox. Only in Electron v100 if that helps.

Please let me know if you need more details and I can provide them. Thanks.

@willoliveira-air it definitely provides some context clues. The big difference here between what you are seeing vs something like @danfooks is the Resize Observer error seems to be being thrown in the test code itself. Are you able to provide a single test in a reproduction repository that has the intermittent failure you are seeing in electron 100? Since I am struggling to reproduce the issue, a reproduction would be immensely helpful to really understanding maybe why this is happening.

danfooks commented 2 years ago

@AtofStryker I think we are good to close this one out! And to respond to your other message, yes, the ResizeObserver error was being thrown in the Cypress test runner itself, rather than in the browser, as far as I know.

AtofStryker commented 2 years ago

@danfooks Perfect. I am going to close this out.

AtofStryker commented 2 years ago

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

rogoit commented 2 years ago

@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);
        });
    });
}

Works fine for me thx for your passion

ryanborhoo commented 1 year ago

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;
    });

This will ignore the failing test, which is not what we want to have.

AtofStryker commented 1 year ago

@ryanborhoo this was recommended to be diagnostic to verify if the error was coming from the test runner or the application. This is not as a solution to this issue. If you are running into an issue where the app is throwing this, you should be able to ignore it through the uncaught:exception handler. But if the test runner is throwing this for you, can you open a new issue with a reproduction?

agugut-nexgen commented 1 year ago

Hello Team I'm having the same issue with cypress 12.15.0 I have this config // Add this uncaught to avoid chrome error on pre prod Cypress.on('uncaught:exception', (err) => { const resizeObserverLoopErrRe = /^[^(ResizeObserver loop limit exceeded)]/; if ( err.message.includes("Cannot set property 'status' of undefined") || err.message.includes("Cannot read property 'method' of undefined") || err.message.includes('Cannot set properties of undefined') || err.message.includes('Cannot read properties of undefined') || err.message.includes('timeLeftSeconds') || resizeObserverLoopErrRe.test(err.message) ) { // returning false here prevents Cypress from // failing the test return false; } return true; }); And the test is failing i n the after each with timeLeftSeconds

mike-plummer commented 1 year ago

@agugut This issue has been closed for some time and was specifically against an older version of Cypress. If you're experiencing this issue against the latest version of Cypress you should open a new issue and provide all the information requested in the template including Debug Logs and a compete, reproducible example that we can run locally to troubleshoot your problem.

Jai-Gogineni commented 1 year ago

Hi All, I can confirm this issue is still happening with current version of cypress using cypress-BDD.

My /e2e.ts

Cypress.on("uncaught:exception", (err, runnable) => { return false; });

I can definitely say its erroring dev code, but the config to say ignore the error is not doing anything just fails the test.

image
lmiller1990 commented 1 year ago

Please provide a minimal reproduction. I tried to reproduce, works fine for me. I will reopen if you can provide a reproduction and take a look.

Repro: https://github.com/lmiller1990/cypress-test-tiny/compare/master...lmiller1990:cypress-test-tiny:issue-22129?expand=1

image

ferdinandsousa commented 9 months ago

I'm getting this error in a cy.origin block. The Cypress version I'm running is 12.17.4

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

  cy.origin(baseUrl, () => {
      Cypress.on('uncaught:exception', () => false);
      // any application JS errors here cause the test to fail. The uncaught:exception does not trap it!
      cy.get('h1').contains('some text');
    });
trainoasis commented 7 months ago

Also started happening to us recently (we re on v13.6.6),

- click
(uncaught exception)Error: ResizeObserver loop completed with undelivered notifications.

even though we have this in since forever and it worked before:

// Handle exceptions
// https://docs.cypress.io/api/events/catalog-of-events#To-conditionally-turn-off-uncaught-exception-handling-for-a-certain-error
Cypress.on("uncaught:exception", (err, runnable, promise) => {
  // do not stop test execution when the bellow error is encountered
  if (
    err.message.includes(
      "Cannot read properties of null (reading 'message')"
    ) ||
    err.message.includes("ResizeObserver loop limit exceeded")
  ) {
    return false;
  }
  // we still want to ensure there are no other unexpected
  // errors, so we let them fail the test
});

any ideas?

PS: I noticed that the message changed lol, so easy fix. I'm just checking for "ResizeObserver loop" now instead. I just simplified to

const ignoreErrorMessagePartial = [
    "ResizeObserver loop",
    "Cannot read properties of null (reading 'message')",
  ];

  if (ignoreErrorMessagePartial.some((msg) => err.message.includes(msg))) {
    return false;
  }
TenPotatoes commented 5 months ago

I was seeing this on Cypress 9 when I upgraded webpack. Issue was due to webpack showing the dev server error overlay, which can be turned off. https://stackoverflow.com/questions/75774800/how-to-stop-resizeobserver-loop-limit-exceeded-error-from-appearing-in-react-a