cypress: Cypress failing after uncaught:exception thrown from 3rd party, even thought 'uncaught:exception' handler is listening.

Current behavior:

On the initial spec run, with a new browser, the exception is thrown from my application

TypeError: Cannot read property 'page_type' of undefined at getPageType

If I rerun the test, without closing the browser, the test passes and the error is not thrown.

I don’t expect you to solve my applications errors. However, this does not occur in a local chrome window, might be useful.

The real issue is when the uncaught exception is thrown by my application it stops Cypress from executing the test and any other tests. However, the page still loads. (See the video attached)

If I am correct, Cypress should not stop with application errors with

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

in commands.js, which it is

Desired behavior:

Cypress does not stop executing when the application throws an exception.

Additional Info (images, stack traces, etc)

https://youtu.be/m0O_P0alpyc

  • Operating System: Mac
  • Cypress Version: 1.1.2
  • Browser Version: Chrome 62

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 16
  • Comments: 72 (25 by maintainers)

Commits related to this issue

Most upvoted comments

Same here. Getting following error in my application: Uncaught TypeError: Cannot read property 'getElementsByClassName' of null

Every test is failing due to it even though I have Cypress.on('uncaught:exception', () => false);

Can you please fix this issue after 1 year of waiting?

Cypress 3.2.0, tests are failing because of 3rd party scripts errors =(

Any update on this from Cypress team? ETA or something?

@jennifer-shehane Just figured out why some people always has it and others have never faced this issue. Cypress can’t catch exceptions thrown by 3rd party javascript that is loaded from different origin.

Not sure about reproducible example, it might take some time on my side

your issue may be solved by the following workaround, but there are still edge cases: in your spec file or support/index.js:


// ignore uncaught exceptions
Cypress.on('uncaught:exception', (err) => {
  return false
})

// patch Cypress top.onerror
Object.defineProperty(top, 'onerror', {
  value: window.onerror,
})

I can’t believe it’s open for 2 years and there’s still no solution for this bug 😮

I found solution for my problem: I added url of 3rd party from where i received this errors (some different types) to cypress.json file: solution err_example

https://docs.cypress.io/guides/references/configuration.html#Folders-Files https://docs.cypress.io/guides/references/configuration.html#Command-Line Maybe this will help someone!

@maximkoshelenko awesome, I was able to reproduce with this. We’re not catching errors thrown by wrapped setTimeout calls. So I’ll add that to the fixing PR

I’m not sure if this is totally relevant so let me know if not and I’ll make a new issue, but thought I’d mention just in case.

I’ve been facing a very similar issue with exceptions of XHR response objects being undefined and this only arising in beforeAll hooks.

My current working theory is that when a page is loaded and tests and all assertions pass, Cypress moves onto the next test, and kills all pending XHR requests. Resulting in the XHR logic which depends on the Response object to exist (rightly so) fails due to trying to read properties on undefined etc, and the error shows up in the next beforeEach hook when the callback finally fails.

This only ever happens for us as flake on our CI machines so it’s incredibly hard to capture.

@brian-mann is my theory correct that any pending requests are cancelled when assertions pass? Is it possible that pending response callbacks are fired with undefined response objects?

Hi @jennifer-shehane, Is there ANY update on this issue. This is one of the most critical bugs open on this one I guess. It is truly a deal breaker for me as you just can not move forward with testing if something like this breaks your test and does not let the page load.

Does cypress atleast have a hack or a work around?

@jennifer-shehane got it. Just create test like this:

  1. open site http://www.sickchirpse.com/10-of-the-worst-websites-ever/
  2. wait for 10 seconds
Cypress.on('uncaught:exception', (err, runnable) => {
    return false;
});

won’t work here. Cypress crashes with error like:

Error: Script error. (:0)
    at global.onerror (http://www.sickchirpse.com/__cypress/runner/cypress_runner.js:23142:10)

Released in 3.6.0.

I added

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

to support/index.js and tests fail anyways because of app’s runtime exceptions. If I use

Cypress.on('fail', (err, runnable) => {return false});

then tests don’t fail but they also don’t run.

@xlenz tested against the only reproducible test case in this thread (https://github.com/cypress-io/cypress/issues/987#issuecomment-523707229).

Kindly provide yours so we can get this figured out.

Is there any possible workaround/solution for this? Every single one of my tests fails because of this. I’ve implemented them all with plan on solving this as the last thing. I’d be very disappointed if it was all just a waste of time.

I used this piece of code Cypress.on('uncaught:exception', (err, runnable) => { // returning false here prevents Cypress from // failing the test console.log('inside Cypress.on ') return false; })

If it does not work then please check your web application and make sure that your application should not throw any error in the console in dev tool.

Well that’s exactly what this thread is about. The piece of code you’ve pased doesn’t trigger in my case and should. I can’t fix the error because it’s coming from 3rd party lib.

Same thing here (Chrome 73), cypress tests fail due to a 3rd party library error, although I tried placing uncaught:exception handlers in different locations (describe( beforeEach() ), it( cy.on() ), support/index.js Cypress.on()). They never get triggered by my error which is a Uncaught TypeError: Cannot read property 'getBoundingClientRect' of undefined. I want cypress to just ignore this error, which used to work in the past via the handler placed in support/index.js. Would love to be able to do this again as I have no control over the 3rd party library to fix this.

My issue was I did not put

cy.on('uncaught:exception', (err) => {
  expect(err.message).to.include('Ignoring error for now');
  return false;
});

inside of my beforeEach, which was running it too late to catch an error being caused by a third party ads script in the head of the page. This fixed things up for me

context('Page Availability', () => {
  beforeEach(() => {
    cy.on('uncaught:exception', (err) => {
      expect(err.message).to.include('Ignoring error for now');
      return false;
    });
  });
});

Have you tried setting up a .route() to listen to the api/config endpoint and ensuring you .wait() for that endpoint before continuing with the rest of your test steps? Cypress has no way to know that your view depends on this endpoint’s returning otherwise. https://docs.cypress.io/api/commands/wait.html#Alias

Here is a much more in depth explanation on why the uncaught:exception may not be being hit: https://github.com/cypress-io/cypress/issues/1385#issuecomment-369082559

We will need a reproducible example to truly investigate the issue further.

I’m 100% sure the fail event will absolutely be caught because Cypress is failing the test. You either didn’t have dev tools open soon enough or you aren’t adding your event listeners in the right place.

Where are you putting them?

Both

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

Cypress.on('fail', (err) => {
    debugger
})

Did not trigger the debugger

Can you prove that is happening? Put a debugger in the uncaught:exception event handler to prove Cypress is catching this as a failure.

Alternatively just bind to Cypress.on('fail', (err) => debugger) and this will show you the exact error and stack trace wheret his originated.