cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Faulty "uncaught expection" trigger in failing expect() in on('window:alert)

Open actuallymentor opened this issue 3 years ago • 4 comments

Current behavior

This assertion fails:

cy.on( 'window:alert', response => {
    expect( response ).to.contain( 'Invalid secret code' )
} )

Results in:

The following error originated from your application code, not from Cypress. It was caused by an unhandled promise rejection.

This is not correct. It is not an application error, it is a cypress assertion.

image

Desired behavior

Test should fail gracefully.

Test code to reproduce

Clone and install dependencies:

https://github.com/actuallymentor/cypress-test-tiny-uncaught-bug.git
npm i
cd bug
npm i
cd ..

Inside ./bug/ run npm start Then inside . run npm run cypress:open

Your result:

image

Cypress Version

10.7.0

Node version

16

Operating System

macOS 12

Debug Logs

No response

Other

No response

actuallymentor avatar Sep 05 '22 10:09 actuallymentor

@actuallymentor Thank you for opening an issue with a code snippet and clear error of the issue you are experiencing! It is super helpful with identifying what the root issue is.

Unfortunately I have not been able to reproduce the issue. Using Cypress 10.7.0, I verified the following example works as expected in Chrome 104, Chrome Beta 106, Edge 105, Electron 102 and Firefox 101.

it('can assert on window.alert', (done) => {
  cy.on( 'window:alert', response => {
    expect( response ).to.contain( 'Invalid secret code' )
    done()
  })
  cy.window().then((win) => {
    win.alert('derp')
  })
})

Image

Is there any other information I need to repo this?

emilyrohrbough avatar Sep 06 '22 20:09 emilyrohrbough

Are you using an M1 mac? The repo should show the issue verbatim (it does for me).

actuallymentor avatar Sep 07 '22 12:09 actuallymentor

@actuallymentor I am on a Mac (macOS Monterey 12.5.1) but its not an M1.

emilyrohrbough avatar Sep 09 '22 18:09 emilyrohrbough

I was able to reproduce the issue with the given repo. We'll route this to a team to look into.

chrisbreiding avatar Sep 16 '22 14:09 chrisbreiding

Update: I'm seeing other cases of failed assertions triggering uncaught exceptions. This time in Github Actions:

2) Name of my section
       Name of my test
     AssertionError: The following error originated from your application code, not from Cypress. It was caused by an unhandled promise rejection.
  > expected 'a string I have' to include 'a substring I asked for'

actuallymentor avatar Oct 04 '22 19:10 actuallymentor