Skip to content

Commit

Permalink
feat: support cy.not previous command (#76)
Browse files Browse the repository at this point in the history
* feat: support cy.not previous command

* update Cy v11
  • Loading branch information
bahmutov authored Jan 19, 2024
1 parent 1e9833e commit c734ee4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Tested with `cy.get`, `cy.contains`, `cy.find`, `.then`, `.within` commands in C

## ⚠️ Warning

In general, Cypress team considers [conditional testing an anti-pattern](https://on.cypress.io/conditional-testing). Thus `cypress-if` should be used only if the test really cannot deterministically execute its steps.
In general, Cypress team considers [conditional testing an anti-pattern](https://on.cypress.io/conditional-testing). Thus `cypress-if` should be used only if the test really cannot deterministically execute its steps. You can also read my [conditional testing](https://glebbahmutov.com/cypress-examples/recipes/conditional-testing.html) examples.

## No xpath support

Expand Down
18 changes: 18 additions & 0 deletions cypress/e2e/exists.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,21 @@ it('uses exists as an alias to exist', () => {
cy.get('@if').should('have.been.calledOnce')
cy.get('@else').should('not.have.been.called')
})

it('supports cy.not', () => {
cy.log('**IF path**')
cy.get('#fruits li')
.not(':odd')
.if('exists')
.log('found even')
.else()
.raise('it is odd')

cy.log('**ELSE path**')
cy.get('#fruits li')
.not('li')
.if('exists')
.raise('cy.not is not supported')
.else()
.log('cy.not is supported')
})
23 changes: 23 additions & 0 deletions src/index-v11.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,29 @@ Cypress.Commands.overwrite(
},
)

Cypress.Commands.overwrite('not', function (notCommand, prevSubject, selector) {
debug('cy.not args', { prevSubject, selector })

const cmd = cy.state('current')
debug(cmd)
const next = cmd.attributes.next

if (next && next.attributes.name === 'if') {
// disable the built-in assertion
return notCommand(prevSubject, selector).then(
(getResult) => {
debug('internal cy.not result', getResult)
return getResult
},
(noResult) => {
debug('no cy.not result', noResult)
},
)
}

return notCommand(prevSubject, selector, text, options)
})

Cypress.Commands.overwrite(
'find',
function (find, prevSubject, selector, options) {
Expand Down
23 changes: 23 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,27 @@ if (major < 12) {
)
throw e
})

Cypress.Commands.overwriteQuery('not', function (notCommand, selector) {
debug('cy.not args', { selector })

const cmd = cy.state('current')
debug(cmd)
const next = cmd.attributes.next
const innerFn = notCommand.call(this, selector)

if (isIfCommand(next)) {
// disable the built-in assertion
return (subject) => {
const res = innerFn(subject)
if (res.length) {
debug('internal not result', res)
return res
}
debug('no not result')
}
}

return (subject) => innerFn(subject)
})
}

0 comments on commit c734ee4

Please sign in to comment.