Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress Uncaught Assertion Error despite cy.on('uncaught:exception')

Tags:

cypress

In relation to the following error:

Uncaught Error: Script error.
Cypress detected that an uncaught error was thrown from a cross origin script.
We cannot provide you the stack trace, line number, or file where this error occurred.

Referencing https://docs.cypress.io/api/events/catalog-of-events.html#To-catch-a-single-uncaught-exception

I am trying to run a test that fills out a form and clicks the button to submit:

it('adds biological sample with maximal input', function(){
    cy.on('uncaught:exception', (err, runnable) => {
    expect(err.message).to.include('of undefined')
      done()
      return false
    });
    cy.get('a').contains('Add biological sample').click();

 . . . 

    cy.contains('Action results');
  });

I get an error despite my spec containing the following:

    cy.on('uncaught:exception', (err, runnable) => {
    expect(err.message).to.include('of undefined')
      done()
      return false
    });

Here's an image of the test failing the test failing.

The error in the bottom left reads,

Error: Uncaught AssertionError: expected '$f is not defined\n\nThis error originated from your application code, not from Cypress. \n\nWhen Cypress detects uncaught errors originating from your application it will automatically fail the current test.\n\nThis behavior is configurable, and you can choose to turn this off by listening to the \'uncaught:exception\' event.\n\nhttps://on.cypress.io/uncaught-exception-from-application' to include 'of undefined' (https://www.flukebook.org/_cypress/runner/cypress_runner.js:49186)

It seems that I am taking Cypress's advice and not getting the desired result. Any suggestions? Has this happened to anyone else?

like image 986
Atticus29 Avatar asked Dec 19 '18 06:12

Atticus29


People also ask

How do I fix uncaught exception in Cypress?

The easiest way to fix this is to add the following to the top of your spec: Cypress. on('uncaught:exception', (err, runnable) => { return false; });

What is uncaught exception in Cypress?

Description: Fires when an uncaught exception occurs in your application. Cypress will fail the test when this fires. Return false from this event and Cypress will not fail the test. Also useful for debugging purposes because the actual error instance is provided to you.

How do I stop error checking in Cypress?

If you'd like to force Cypress to interact with the element there are a few options: Pass {force: true} . This disables all error checking.

What is uncaught exception?

June 01, 2022. CWE 248-Uncaught Exception occurs when an exception is not caught by a programming construct or by the programmer, it results in an uncaught exception. In Java, for example, this would be an unhandled exception that would terminate the program.


2 Answers

Can you please remove expect(err.message).to.include('of undefined') and done() from the cypress exception block and add the below piece of code inside the test & run the test again

Cypress.on('uncaught:exception', (err, runnable) => {
    // returning false here prevents Cypress from
    // failing the test
    return false
})
like image 144
soccerway Avatar answered Sep 17 '22 18:09

soccerway


The easiest way to fix this is to add the following to the top of your spec:

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

This gets the same indentation level as your "it" blocks, nested directly under "describe". It will cause cypress to ignore all uncaught JS exceptions.

In the question, Atticus29 expects "of undefined" to be present in the error message, but the error doesn't actually contain that string. He could change

expect(err.message).to.include('of undefined')

to

expect(err.message).to.include('is not defined')

then it will pass.

To turn off all uncaught exception handling in a spec (recommended) https://docs.cypress.io/api/events/catalog-of-events.html#To-turn-off-all-uncaught-exception-handling

To catch a single uncaught exception and assert that it contains a string https://docs.cypress.io/api/events/catalog-of-events.html#To-catch-a-single-uncaught-exception

like image 41
emery Avatar answered Sep 17 '22 18:09

emery