cypress
cypress copied to clipboard
Faulty "uncaught expection" trigger in failing expect() in on('window:alert)
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.

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:

Cypress Version
10.7.0
Node version
16
Operating System
macOS 12
Debug Logs
No response
Other
No response
@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')
})
})

Is there any other information I need to repo this?
Are you using an M1 mac? The repo should show the issue verbatim (it does for me).
@actuallymentor I am on a Mac (macOS Monterey 12.5.1) but its not an M1.
I was able to reproduce the issue with the given repo. We'll route this to a team to look into.
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'