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)
- 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
- workaround for cypress unhandled errors, see https://github.com/cypress-io/cypress/issues/987 — committed to kahboom/syndesis by kahboom 5 years ago
Same here. Getting following error in my application:
Uncaught TypeError: Cannot read property 'getElementsByClassName' of nullEvery 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: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:

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
setTimeoutcalls. So I’ll add that to the fixing PRI’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
beforeAllhooks.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
undefinedetc, 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:
won’t work here. Cypress crashes with error like:
Released in
3.6.0.I added
to support/index.js and tests fail anyways because of app’s runtime exceptions. If I use
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.
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:exceptionhandlers in different locations (describe( beforeEach() ),it( cy.on() ),support/index.js Cypress.on()). They never get triggered by my error which is aUncaught 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 insupport/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
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 meHave you tried setting up a
.route()to listen to theapi/configendpoint 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#AliasHere 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
failevent 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
Did not trigger the debugger
Can you prove that is happening? Put a debugger in the
uncaught:exceptionevent 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.