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

Uncaught Exception not failing a test #1385

Closed
alurim opened this issue Feb 28, 2018 · 11 comments
Closed

Uncaught Exception not failing a test #1385

alurim opened this issue Feb 28, 2018 · 11 comments

Comments

@alurim
Copy link

alurim commented Feb 28, 2018

  • Operating System: Mac
  • Cypress Version: 2.0.4
  • Browser Version: Chrome Version 64.0.3282.186

Is this a Feature or Bug?

Bug... I think?

Current behavior:

Added a setTimeout(() => { throw new Error('Whoops!') }, 200) in my application code and ran a test that took a couple of seconds to run and it successfully completed.

Desired behavior:

I was under the impression that any uncaught exception will cause any test that's running to fail. If I was wrong is there any way to ensure that the test fails? I tried adding the following code but the debugger never fired:

    cy.on('uncaught:exception', (err, runnable) => {
      debugger
    })
    cy.on('window:alert', (err, runnable) => {
      debugger
    })

How to reproduce:

  • Add setTimeout(() => { throw new Error('Whoops!') }, 200) to any application code
  • Run a test that take a couple of seconds to ensure that error gets thrown
  • See the test succeed instead of fail.

Additional Info (images, stack traces, etc)

2

@brian-mann
Copy link
Member

brian-mann commented Feb 28, 2018

Cypress will absolutely catch uncaught exceptions and fail the current test.

There are lots of tests around this behavior. What I imagine is happening here is some kind of edge case either that we didn't consider or that this isn't a real uncaught exception.

We will not catch errors if:

  • The test has completed (which means there is no test to associate to)
  • The error was thrown in an iframe or anything other than the window Cypress is bound to
  • It's not really an uncaught exception - where it gets handled by your app and then console.error(err) which makes it look like its uncaught but its really not.

What we'd need you to do is reduce down your example to whatever is the simplest to recreate. For instance I can see you are using HMR. If HMR is configured to work through an iframe, that could be the source.

You can look at our own tests as reference. To test uncaught exception handling - just throw an error directly in an <script> tag inside of an <html> file that you use with cy.visit(). You don't even need anything else.

Since uncaught exceptions can handle in a ton of different ways, we wrote tests that express them in all kinds of circumstances.

For instance they could happen:

  • in your spec code
  • in your app code
  • synchronously outside of a test
  • asynchronously outside of a test
  • synchronously in a test
  • asynchronously in a test
  • synchronously in a hook
  • asynchronously in a hook

All of this behavior is tested in the driver code and in the e2e tests that run Cypress in a 'pure' form the same as you run your projects.

@brian-mann brian-mann added the stage: needs information Not enough info to reproduce the issue label Feb 28, 2018
@brian-mann
Copy link
Member

Closing because we've not heard back and nobody has provided a reproducible case.

@alexindigo
Copy link

@brian-mann I have something similar, there is JS error happening when I do cy.go('back'),
and cypress somehow ignores it, and continues as normal.

Uncaught Exception

Is there anything else I can provide to help resolve the issue?

Thank you.

@jennifer-shehane
Copy link
Member

@alexindigo Hey, giving us a reproducible issue is the best way to help resolve an issue.

@andezzat
Copy link
Contributor

andezzat commented Dec 3, 2018

Reproducible & super simplified version right here: #2487 (comment)

Someone please give it a look, because this issue just keeps happening in our app that we test in Cypress and it's causing me so much pain :(

@jennifer-shehane jennifer-shehane removed stage: needs information Not enough info to reproduce the issue labels Jan 16, 2019
@daniel-kun
Copy link

Hey @jennifer-shehane, I have a reproducible, very simple test case here:

describe("Visiting IFTTT", function() {
    it("should ignore the occuring JavaScript error", function() {
        cy.viewport(1024, 880);

        cy.on('uncaught:exception', (err, runnable) => {
            // returning false here prevents Cypress from
            // failing the test
            console.log("Catched exception");
            return false;
        });
        cy.visit("https://ifttt.com/");
        cy.get('a.sign-in').click();
    });
});

The result is (currently, it might change if IFTTT works on their site, of course) nearly 100% that there will be a JavaScript error that lets the test fail, although there's a cy.on('uncaught:exception'... there. With Cypress.on... I have the same results.

Here is a video of the failure:
https://1drv.ms/v/s!AiPF014W0FYfv8cXGBXxJ8PyToPHHw?e=cHhzsd

I'm using Cypress 3.4.0 on Windows, installed via npm install and run via .\node_modues\.bin\cypress.

Hope this helps to pinpoint the issue.

@Colmea
Copy link

Colmea commented Sep 24, 2019

Hi,

@brian-mann Just to be clear, when you say:

We will not catch errors if [...] The test has completed (which means there is no test to associate to)

it means that it will not work (or be flaky) if the cy.on('uncaught:exception'... is the last line of the test ?

Example:

// This action will trigger an Exception
cy.get('#myButton').click();

// Catch Exception AFTER the actual action
cy.on('uncaught:exception', (err, runnable) => {
  return false;
});

This won't work ?

@swathisham
Copy link

any update on this issue?

@andezzat
Copy link
Contributor

@swathisham

#2487 (comment)

I'm tracking this issue instead ☝️

@swathisham
Copy link

swathisham commented Dec 29, 2019

I am getting assertion error. But test passed even though it shows assertion error

@jennifer-shehane
Copy link
Member

Please follow this issue or open a new issue with a completely reproducible example #2487

@cypress-io cypress-io locked and limited conversation to collaborators Dec 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants