Many people complain that the Cypress cy.then command is confusing. It looks like a Promise, yet it is not.
- you cannot replace it with
async / awaitsyntax sugar
- it behaves differently from the
Promise.then, for example if the callback returns undefined value, then the original subject value is passed to the next command or assertion
To avoid the confusion, you can disallow using the
cy.then command completely.
// if anyone tries to use cy.then in the spec code or in a plugin
We still want the functionality of the
cy.then command, but under the less confusing name. We do need the original
cy.then command functionality. Thus we will grab the reference to the function so we can call it from our command. We can find the reference by inspecting the
Let's add a custom command
cy.later that calls the banned
cy.then command function.
// find and save the reference to the original cy.then command
The new command is working as expected, but hopefully does not lead the users down the wrong path.
I hope this helps.
🎁 You can find the above code in a recipe at glebbahmutov.com/cypress-examples.