Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
John758364 opened this issue Nov 30, 2017 · 72 comments · Fixed by #5249
Assignees
Labels
existing workaround topic: unhandled exceptions ⚠️ Issues involving unhandled or uncaught exception handling type: bug

Comments

@John758364
Copy link

John758364 commented Nov 30, 2017

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
@brian-mann
Copy link
Member

Okay so there are two issues here:

  1. That Cypress is stopping after your test fails. This is actually not a bug. The error itself tells you exactly why Cypress is stopping. It's because an error occurred in a before each hook. When that happens it makes no sense to test any of the subsequent tests (because they are all guaranteed to fail).

screen shot 2017-11-30 at 1 34 53 pm

  1. That there is an error at all happening. The thing is, from looking at the error message I can tell that this is not an uncaught error from your application. Unfortunately it's not clear from the stack trace or anything. Try clicking on the error message and looking and inspecting the stack in your console. Maybe that will provide more information

@John758364
Copy link
Author

@brian-mann Sorry, the uncaught exception is being thrown by my application. However Cypress should not stop because of that, I update my issue.

@John758364
Copy link
Author

I know why the error is being thrown on my application, kind of. I know the line it is breaking on and why. Not exactly sure on why the code isn't running though.

If you want I can post that information also?

@brian-mann
Copy link
Member

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.

@John758364
Copy link
Author

John758364 commented Nov 30, 2017

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

@brian-mann
Copy link
Member

did you have dev tools open before the tests ran?

@brian-mann
Copy link
Member

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?

@Gennadiii
Copy link

Gennadiii commented Apr 16, 2018

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.

@asos-arun
Copy link

I am facing same problem.
image
Commands.js
image

@jennifer-shehane
Copy link
Member

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.

@asos-arun @Gennadiii Could you both also follow these directions by adding the debugger?

@arun290636
Copy link

arun290636 commented Apr 18, 2018

@jennifer-shehane Thanks a lot for your quick response . I have tried with below code and its not working.

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

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

Let me know if i am missing anything?

@jennifer-shehane
Copy link
Member

Here is a much more in depth explanation on why the uncaught:exception may not be being hit: #1385 (comment)

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

@Gennadiii
Copy link

Gennadiii commented Apr 19, 2018

In my case I get error in Cypress: Cannot read property 'payload' of undefined. But weird thing is that I don't see this error in console. It is caught by Cypress and I can see it in console only if I click it in Cypress panel.
url
url
This code in support/index.js doesn't work:

Cypress.on('uncaught:exception', (err, runnable) => {
  console.log(`11111111111111111111111 ${err}`);
  debugger;
  return false;
});

No log, no debugger.

This code works as expected:

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

But doesn't do what I need to do.
I can't provide environment and exact code since it's a commercial project.

@brian-mann
Copy link
Member

@Gennadiii We are a small team and have invested a lot of time into this issue. We believe this is a problem with Cypress, but we are unable to reproduce or recreate. We will have to have a reproducible repo in order to get this fixed. We will keep this issue open as reference until someone provides how this is happening.

Since you expressed hesitation to provide a reproducible repo because you're working on a commericial project, please note that we do offer premium support for users to prioritize bug fixes, do screensharing, and code reviews. If you're interested in this kind of premium support, we can look directly at how/why this is happening. Please let us know by emailing [email protected].

@Gennadiii
Copy link

@brian-mann Thanks for your suggestion we will consider the support option. Meanwhile I have some more info that might help on this one. There are some http queries that site makes.
When everything is fine:
2018-04-24 12_43_37-integration
2018-04-24 12_45_28-integration
But sometimes one query doesn't get any response at all. And next test fails.
2018-04-24 12_43_29-integration
2018-04-24 12_44_07-integration

@jennifer-shehane
Copy link
Member

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

@A-Industry
Copy link

Hi, I have a similar problem. I request my application with cy.visit('/'). The application starts fetching data, but most of the times this will result in a 401. (even on the login page). This is expected behaviour, but catching the error with Cypress and returning false still results in the tests not continuing.
Is there a way to recover from an XHR error? This should not affect my tests

@spencer1573
Copy link

I'm dealing with the same issue i think. i can't get it to do a cy.log when it throws an XHR request error. you can see my issue (different issue?) here: #1710

@xlenz
Copy link

xlenz commented Dec 5, 2018

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?

@kelsonic

This comment has been minimized.

@bmarti44
Copy link

bmarti44 commented Dec 13, 2018

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

@jennifer-shehane
Copy link
Member

As @bmarti44 stated - please ensure you have your listener set up properly to catch uncaught exceptions within Cypress.

Unfortunately we'll have to close this issue if no reproducible example is provided. Can anyone provide a way to reproduce this?

@jennifer-shehane jennifer-shehane added the stage: needs information Not enough info to reproduce the issue label Dec 18, 2018
@jennifer-shehane
Copy link
Member

@mgrybyk Maybe I've missed it. Could you point me to the exact application code and test code that I can run locally on my machine to produce this error?

@mgrybyk
Copy link

mgrybyk commented Jan 8, 2019

@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

@mgrybyk
Copy link

mgrybyk commented Jan 8, 2019

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

@jennifer-shehane
Copy link
Member

Thanks so much @mgrybyk for providing a reproducible example.

Failing example

it('Does not catch script error', () => {
  Cypress.on('uncaught:exception', (err, runnable) => {
    throw err
  })

  cy.visit('http://www.sickchirpse.com/10-of-the-worst-websites-ever/')
})

@jennifer-shehane jennifer-shehane added stage: ready for work The issue is reproducible and in scope and removed stage: needs information Not enough info to reproduce the issue labels Jan 24, 2019
@kuceb
Copy link
Contributor

kuceb commented Oct 15, 2019

@automationJatinder and Cypress.on('uncaught:exception') is not fired?

@maximkoshelenko
Copy link

maximkoshelenko commented Oct 21, 2019

Any news about fixing this one?
Both handlers added to support/index but didnt catch the error...

Cypress.on('uncaught:exception', (err, runnable) => false);
Object.defineProperty(top, 'onerror', {
    value: window.onerror,
})

image

@automationJatinder
Copy link

@automationJatinder and Cypress.on('uncaught:exception') is not fired?

No.

@maximkoshelenko Please share your code where you are facing error. I think I have solution for the same.

@maximkoshelenko
Copy link

@maximkoshelenko Please share your code where you are facing error. I think I have solution for the same.

@automationJatinder Thanks. Here you go
shortCypress.zip
I have copied the same test a couple of times because the error may occur or may not occur during one execution.

@kuceb
Copy link
Contributor

kuceb commented Oct 23, 2019

@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

@maximkoshelenko
Copy link

@bkucera Super. Hope fix will be published soon :) my POC project are burning because of that issue.

@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: needs review The PR code is done & tested, needs review stage: work in progress labels Oct 30, 2019
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Oct 30, 2019
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 30, 2019

The code for this is done in cypress-io/cypress#5249, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 5, 2019

Released in 3.6.0.

@azaeng04
Copy link

azaeng04 commented Mar 3, 2020

@jennifer-shehane I have been following this thread and I am still experiencing this issue in version 4.0.0. I noticed that it is pointing out issues in node_modules in node_modules which doesn't make sense. We successfully used our custom npm package on our api tests.

@bahmutov
Copy link
Contributor

bahmutov commented Mar 3, 2020

@azaeng04 if you are experiencing this issue, please open a new issue with fully reproducible example we can run

@azaeng04
Copy link

azaeng04 commented Mar 3, 2020

@bahmutov I can show an image of what I am seeing and I can mention the node_module where the error is being thrown

2020-03-03_21h36_42

2020-03-03_21h36_52

@azaeng04
Copy link

azaeng04 commented Mar 3, 2020

@bahmutov I can show an image of what I am seeing and I can mention the node_module where the error is being thrown

Not sure why it would be pointing to a node_module in the node_modules? And the fs-extra package to be exact.

@bahmutov
Copy link
Contributor

bahmutov commented Mar 3, 2020 via email

@azaeng04
Copy link

azaeng04 commented Mar 16, 2020

@bahmutov it seems to be pointing out errors in the fs-extra package. Just calling fs.copy throws the following error:

Uncaught (in promise) TypeError: fs.stat is not a function

This package is in a custom package of ours and Cypress seems to throw an error and fail because of a variable(s) that is not a function as per the above.

Help on this would be much appreciated @jennifer-shehane @bahmutov @brian-mann

@bahmutov
Copy link
Contributor

@azaeng04 this issue has been closed, so any comments here are usually non-productive. Second, your issue seems to be unrelated to the original - it seems you are trying to run Node file commands from Cypress tests which is impossible (Cypress tests run in the browser). If you want to use fs-extra package, please move these commands to plugins file and call them using https://on.cypress.io/task command

@jennifer-shehane
Copy link
Member

This issue will be closed to further comment as the exact issue here was resolved and tested in 3.6.0.

If you're experiencing a bug similar to this in Cypress, please open a new issue with a fully reproducible example that we can run. There may be a specific edge case with the issue that we need more detail to fix.

Also, check out our community chat, it can be helpful for debugging or answering questions on how to use Cypress.

@cypress-io cypress-io locked as resolved and limited conversation to collaborators Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.