cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Unable to handle uncaught:exception within test

Open steinybot opened this issue 1 year ago • 1 comments

Current behavior

The instructions on https://docs.cypress.io/api/cypress-api/catalog-of-events#To-catch-a-single-uncaught-exception on how to catch a single uncaught exception from the app do not work. uncaught:exception is being given the cypress runner error and not the uncaught error from the application.

  1) page
       works:

      The following error originated from your test code, not from Cypress.

  > expected 'The following error originated from your application code, not from Cypress.\n\n  > Uncaught Muahahaha!\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.' to equal 'Error: Uncaught Muahahaha!'

When Cypress detects uncaught errors originating from your test code it will automatically fail the current test. (and Mocha's done() called multiple times)
      + expected - actual

      -'The following error originated from your application code, not from Cypress.\n\n  > Uncaught Muahahaha!\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.'
      +'Error: Uncaught Muahahaha!'

  AssertionError: The following error originated from your test code, not from Cypress.

    > expected 'The following error originated from your application code, not from Cypress.\n\n  > Uncaught Muahahaha!\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.' to equal 'Error: Uncaught Muahahaha!'

  When Cypress detects uncaught errors originating from your test code it will automatically fail the current test.
      at $Cy.eval (webpack://cypress-test-tiny/./cypress/e2e/spec.cy.js:7:29)

Desired behavior

uncaught:exception should be given the uncaught error from the app like the docs say.

Test code to reproduce

https://github.com/steinybot/cypress-test-tiny/tree/uncaught-exception-in-test

import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <button onClick={() => {throw "Muahahaha!"}}>Click</button>
      </header>
    </div>
  );
}

export default App;
/// <reference types="cypress" />
describe('page', () => {
  it('works', (done) => {
    // this event will automatically be unbound when this
    // test ends because it's attached to 'cy'
    cy.on('uncaught:exception', (err, runnable) => {
      expect(err.message).to.equal('Error: Uncaught Muahahaha!')

      // using mocha's async done callback to finish
      // this test so we prove that an uncaught exception
      // was thrown
      done()

      // return false to prevent the error from
      // failing this test
      return false
    })

    cy.visit('/')
    cy.get('button').click()
  })
})

Cypress Version

13.7.0

Node version

21.7.1

Operating System

macOS 14.2.1

Debug Logs

No response

Other

No response

steinybot avatar Mar 14 '24 02:03 steinybot

This used to work for us, but in the past few months we now see the Cypress error wrapping our error. I'm not positive if it used to only be our error that came through to the callback, or if our message was being unwrapped.

That is, I'm not sure if exact equals on err.message was working because our test was doing expect(err.message).to.contain('Failed to load Google Captcha API') and that was working. For the past couple months we've been seeing:

expected 'The following error originated from your application code, not from Cypress. It was caused by an unhandled promise rejection.\n\n > An unknown error has occurred: [object Event]\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.' to include 'Failed to load Google Captcha API'

Which, that'd be fine, except for some reason our message gets converted to [object Event]. 🤔

OlsonDev avatar Jun 28 '24 05:06 OlsonDev

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

cypress-app-bot avatar Dec 26 '24 01:12 cypress-app-bot

This issue has been closed due to inactivity.

cypress-app-bot avatar Jan 09 '25 02:01 cypress-app-bot