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

fix: actionability not retrying in after hooks where the test failed #30831

Merged
merged 13 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

_Released 2/11/2025 (PENDING)_

**Bugfixes:**

- All commands performed in `after` and `afterEach` hooks will now correctly retry when a test fails. Commands that are actions like `.click()` and `.type()` will now perform the action in this situation also. Fixes [#2831](https://github.com/cypress-io/cypress/issues/2831).

**Dependency Updates:**

- Upgraded `mime` from `2.6.0` to `3.0.0`. Addressed in [#30966](https://github.com/cypress-io/cypress/pull/30966).
Expand Down
46 changes: 46 additions & 0 deletions packages/driver/cypress/e2e/commands/actions/click.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,52 @@ describe('src/cy/commands/actions/click', () => {
cy.get('#dom').invoke('css', 'scrollBehavior').then((scrollBehavior) => expect(scrollBehavior).to.eq('smooth'))
})
})

describe('retries in after hook when failures', () => {
it('clicks element in hook', () => {
cy.on('fail', (err) => {
expect(err.message).contain('expected true to be false')
})

expect(true).to.be.false
})

after(() => {
const onClick = cy.stub()

const $button = cy.$$('#button')

$button.on('click', onClick)

cy.get('#button').click().then(() => {
expect(onClick).to.be.calledOnce
})
})
})

describe('retries in afterEach hook when failures', () => {
it('clicks element in hook', () => {
cy.on('fail', (err) => {
expect(err.message).contain('expected true to be false')

return false
})

expect(true).to.be.false
})

afterEach(() => {
const onClick = cy.stub()

const $button = cy.$$('#button')

$button.on('click', onClick)

cy.get('#button').click().then(() => {
expect(onClick).to.be.calledOnce
})
})
})
})

describe('assertion verification', () => {
Expand Down
44 changes: 44 additions & 0 deletions packages/driver/cypress/e2e/commands/actions/type.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,50 @@ describe('src/cy/commands/actions/type - #type', () => {

cy.get(':text:first').type('foo', { scrollBehavior: false, timeout: 200 })
})

describe('retries in after hook when failures', () => {
it('types in element in hook', () => {
cy.on('fail', (err) => {
expect(err.message).contain('expected true to be false')
})

expect(true).to.be.false
})

after(() => {
const input = cy.$$('input:text:first')

input.val('')

expect(input).to.have.value('')

cy.get('input:text:first').type('foo').then(($input) => {
expect($input).to.have.value('foo')
})
})
})

describe('retries in afterEach hook when failures', () => {
it('types in element in hook', () => {
cy.on('fail', (err) => {
expect(err.message).contain('expected true to be false')
})

expect(true).to.be.false
})

afterEach(() => {
const input = cy.$$('input:text:first')

input.val('')

expect(input).to.have.value('')

cy.get('input:text:first').type('foo').then(($input) => {
expect($input).to.have.value('foo')
})
})
})
})

describe('input types where no extra formatting required', () => {
Expand Down
5 changes: 2 additions & 3 deletions packages/driver/src/cy/retries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ export const create = (Cypress: ICypress, state: StateFunc, timeout: $Cy['timeou
const ended = () => {
// we should NOT retry if
// 1. our promise has been canceled
// 2. or we have an error
// 3. or if the runnables has changed
// 2. or if the runnables has changed

// although bluebird SHOULD cancel these retries
// since they're all connected - apparently they
Expand All @@ -116,7 +115,7 @@ export const create = (Cypress: ICypress, state: StateFunc, timeout: $Cy['timeou
// bug in bluebird with not propagating cancellations
// fast enough in a series of promises
// https://github.com/petkaantonov/bluebird/issues/1424
return state('canceled') || state('error') || runnableHasChanged()
return state('canceled') || runnableHasChanged()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea if this code have any impact on before/beforeEach blocks? We have quite a few tests for this I believe in https://github.com/cypress-io/cypress/blob/after-hook-action-fix/packages/app/cypress/e2e/runner/runner.mochaEvents.cy.ts so it appears to not. Usually if the before/beforeEach fails I believe we skip the suite but I don't think that marks the runnable as canceled, but has the same effect?

I don't think we are going to know the true impact of this until we release it to the general public which might introduce the use/test case we are missing here... or it may not introduce anything at all except a fix!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't have any negative side effects because the code running in hook runnables is (and should be running) - its just that commands weren't retrying.

}

return Promise
Expand Down
Loading