mirror of
https://github.com/cypress-io/cypress.git
synced 2026-01-30 02:54:27 -06:00
Add better handling for errors thrown in fail handler (#7513)
This commit is contained in:
@@ -749,6 +749,14 @@ const create = function (specWindow, Cypress, Cookies, state, config, log) {
|
||||
throw err
|
||||
}
|
||||
|
||||
// this means the error came from a 'fail' handler, so don't send
|
||||
// 'cy:fail' action again, just finish up
|
||||
if (err.isCyFailErr) {
|
||||
delete err.isCyFailErr
|
||||
|
||||
return finish(err)
|
||||
}
|
||||
|
||||
// if we have a "fail" handler
|
||||
// 1. catch any errors it throws and fail the test
|
||||
// 2. otherwise swallow any errors
|
||||
@@ -764,9 +772,9 @@ const create = function (specWindow, Cypress, Cookies, state, config, log) {
|
||||
rets = Cypress.action('cy:fail', err, state('runnable'))
|
||||
} catch (cyFailErr) {
|
||||
// and if any of these throw synchronously immediately error
|
||||
cyFailErr.stack = $stackUtils.normalizedStack(cyFailErr)
|
||||
cyFailErr.isCyFailErr = true
|
||||
|
||||
return finish(cyFailErr)
|
||||
return fail(cyFailErr)
|
||||
}
|
||||
|
||||
// bail if we had callbacks attached
|
||||
|
||||
@@ -15,7 +15,7 @@ const onServer = function (app) {
|
||||
return app.get('/response', (req, res) => res.json({ ok: true }))
|
||||
}
|
||||
|
||||
const VARIOUS_FAILURES_EXPECTED_FAILURES = 61
|
||||
const VARIOUS_FAILURES_EXPECTED_FAILURES = 63
|
||||
|
||||
const verifyPassedAndFailedAreSame = (expectedFailures) => {
|
||||
return ({ stdout }) => {
|
||||
|
||||
@@ -705,6 +705,30 @@ context('event handlers', function () {
|
||||
message: 'bar is not a function',
|
||||
})
|
||||
})
|
||||
|
||||
describe('fail handler assertion failure', function () {
|
||||
fail(this, () => {
|
||||
cy.failFailHandlerAssertion()
|
||||
})
|
||||
|
||||
verify(this, {
|
||||
column: 25,
|
||||
codeFrameText: 'failFailHandlerAssertion',
|
||||
message: `expected 'actual' to equal 'expected'`,
|
||||
})
|
||||
})
|
||||
|
||||
describe('fail handler exception', function () {
|
||||
fail(this, () => {
|
||||
cy.failFailHandlerException()
|
||||
})
|
||||
|
||||
verify(this, {
|
||||
column: 10,
|
||||
codeFrameText: 'failFailHandlerException',
|
||||
message: 'bar is not a function',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
context('uncaught errors', () => {
|
||||
|
||||
@@ -835,6 +835,36 @@ context('event handlers', function () {
|
||||
message: 'bar is not a function',
|
||||
})
|
||||
})
|
||||
|
||||
describe('fail handler assertion failure', function () {
|
||||
fail(this, () => {
|
||||
cy.on('fail', () => {
|
||||
expect('actual').to.equal('expected')
|
||||
})
|
||||
|
||||
cy.get('#does-not-exist')
|
||||
})
|
||||
|
||||
verify(this, {
|
||||
column: 29,
|
||||
message: `expected 'actual' to equal 'expected'`,
|
||||
})
|
||||
})
|
||||
|
||||
describe('fail handler exception', function () {
|
||||
fail(this, () => {
|
||||
cy.on('fail', () => {
|
||||
({}).bar()
|
||||
})
|
||||
|
||||
cy.get('#does-not-exist')
|
||||
})
|
||||
|
||||
verify(this, {
|
||||
column: 14,
|
||||
message: 'bar is not a function',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
context('uncaught errors', () => {
|
||||
|
||||
@@ -377,6 +377,22 @@ Cypress.Commands.add('failEventHandlerException', () => {
|
||||
cy.visit('http://localhost:1919')
|
||||
})
|
||||
|
||||
Cypress.Commands.add('failFailHandlerAssertion', () => {
|
||||
cy.on('fail', () => {
|
||||
expect('actual').to.equal('expected')
|
||||
})
|
||||
|
||||
cy.get('#does-not-exist')
|
||||
})
|
||||
|
||||
Cypress.Commands.add('failFailHandlerException', () => {
|
||||
cy.on('fail', () => {
|
||||
({}).bar()
|
||||
})
|
||||
|
||||
cy.get('#does-not-exist')
|
||||
})
|
||||
|
||||
Cypress.Commands.add('failSyncAppException', () => {
|
||||
cy.visit('/js_errors.html')
|
||||
cy.get('.sync-error').click()
|
||||
|
||||
Reference in New Issue
Block a user