Search code examples
cypressuncaught-exception

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


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?


Solution

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