Skip to content

Commit

Permalink
fix: find the subject for the finally, closes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Sep 1, 2022
1 parent 10adba0 commit 638b7e1
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
77 changes: 77 additions & 0 deletions cypress/e2e/finally.cy.js
Expand Up @@ -19,4 +19,81 @@ describe('finally', () => {
.finally()
.should('be.checked')
})

it('executes the IF branch', () => {
cy.wrap(1)
.if('equal', 1)
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
.finally()
.should('equal', 1)
cy.get('@if').should('have.been.calledOnce')
cy.get('@else').should('not.be.called')
})

it('executes the ELSE branch', () => {
cy.wrap(1)
.if('equal', 42)
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
.finally()
.should('equal', 1)
cy.get('@else').should('have.been.calledOnce')
cy.get('@if').should('not.be.called')
})

it('executes the FINALLY command', () => {
cy.wrap(1)
.if('equal', 42)
.then(cy.spy().as('if'))
.else()
.then(cy.spy().as('else'))
.finally()
.then(cy.spy().as('finally'))
cy.get('@else').should('have.been.calledOnce')
cy.get('@if').should('not.be.called')
cy.get('@finally').should('have.been.calledOnce')
})

it('yields the IF subject without ELSE branch', () => {
cy.wrap(1)
.if('equal', 1)
.then((n) => {
expect(n, 'if n').to.equal(1)
console.log('if path, n = %d', n)
return 101
})
.finally()
.should('equal', 101)
})

it('yields the IF subject', () => {
cy.wrap(1)
.if('equal', 1)
.then((n) => {
expect(n, 'if n').to.equal(1)
console.log('if path, n = %d', n)
return 101
})
.else()
.then(() => -1)
.finally()
.should('equal', 101)
})

it('yields the ELSE subject', () => {
cy.wrap(1)
.if('equal', 42)
.then((n) => {
expect(n, 'if n').to.equal(1)
console.log('if path, n = %d', n)
return 101
})
.else()
.then(() => -1)
.finally()
.should('equal', -1)
})
})
24 changes: 20 additions & 4 deletions src/index.js
@@ -1,7 +1,11 @@
const debug = require('debug')('cypress-if')

function skipRestOfTheChain(cmd, chainerId) {
while (cmd && cmd.attributes.chainerId === chainerId) {
while (
cmd &&
cmd.attributes.chainerId === chainerId &&
cmd.attributes.name !== 'finally'
) {
debug('skipping "%s"', cmd.attributes.name)
cmd.attributes.skip = true
cmd = cmd.attributes.next
Expand All @@ -15,6 +19,12 @@ function findMyIfSubject(elseCommandAttributes) {
if (elseCommandAttributes.name === 'if') {
return elseCommandAttributes.ifSubject
}
if (
!elseCommandAttributes.skip &&
!Cypress._.isNil(elseCommandAttributes.subject)
) {
return elseCommandAttributes.subject
}
if (elseCommandAttributes.prev) {
return findMyIfSubject(elseCommandAttributes.prev.attributes)
}
Expand Down Expand Up @@ -74,7 +84,7 @@ Cypress.Commands.add(
debug('else branch starts right away')
nextCommand = null
} else {
debug('skipping "%s"', nextCommand.attributes.name)
debug('am skipping "%s"', nextCommand.attributes.name)
debug(nextCommand.attributes)
nextCommand.attributes.skip = true

Expand All @@ -87,6 +97,7 @@ Cypress.Commands.add(
}

if (subject) {
debug('wrapping subject', subject)
cy.wrap(subject, { log: false })
}
return
Expand Down Expand Up @@ -117,7 +128,7 @@ Cypress.Commands.add(
)

Cypress.Commands.add('else', { prevSubject: true }, (subject) => {
// debug('else command, subject', subject)
debug('else command, subject', subject)
// debug('current subject', cy.currentSubject())
// debug('current command attributes', cy.state('current').attributes)
// console.log('subjects', cy.state('subjects'))
Expand All @@ -131,11 +142,16 @@ Cypress.Commands.add('else', { prevSubject: true }, (subject) => {
})

Cypress.Commands.add('finally', { prevSubject: true }, (subject) => {
debug('finally with the subject', subject)

// notice: cy.log yields "null" 🤯
// https://github.com/cypress-io/cypress/issues/23400
if (typeof subject === 'undefined' || subject === null) {
// find the subject from the "if()" before
subject = findMyIfSubject(cy.state('current').attributes)
const currentCommand = cy.state('current').attributes
debug('current command is finally', currentCommand)
subject = findMyIfSubject(currentCommand)
debug('found subject', subject)
}
if (subject) {
cy.wrap(subject, { log: false })
Expand Down

0 comments on commit 638b7e1

Please sign in to comment.