From 638b7e100a636b1ddbbaf780b5513e2807730ee7 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 1 Sep 2022 07:14:00 -0400 Subject: [PATCH] fix: find the subject for the finally, closes #18 --- cypress/e2e/finally.cy.js | 77 +++++++++++++++++++++++++++++++++++++++ src/index.js | 24 ++++++++++-- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/cypress/e2e/finally.cy.js b/cypress/e2e/finally.cy.js index b866c43..de16184 100644 --- a/cypress/e2e/finally.cy.js +++ b/cypress/e2e/finally.cy.js @@ -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) + }) }) diff --git a/src/index.js b/src/index.js index 66abf77..29750dc 100644 --- a/src/index.js +++ b/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 @@ -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) } @@ -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 @@ -87,6 +97,7 @@ Cypress.Commands.add( } if (subject) { + debug('wrapping subject', subject) cy.wrap(subject, { log: false }) } return @@ -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')) @@ -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 })