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

Unable to handle uncaught:exception within test #29142

Open
steinybot opened this issue Mar 14, 2024 · 1 comment
Open

Unable to handle uncaught:exception within test #29142

steinybot opened this issue Mar 14, 2024 · 1 comment
Labels
stage: needs investigating Someone from Cypress needs to look at this

Comments

@steinybot
Copy link

steinybot commented Mar 14, 2024

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

@jennifer-shehane jennifer-shehane added the stage: needs investigating Someone from Cypress needs to look at this label Apr 15, 2024
@OlsonDev
Copy link

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]. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: needs investigating Someone from Cypress needs to look at this
Projects
None yet
Development

No branches or pull requests

3 participants