Replace cy.then command

Many people have a problem with the cy.thenopen in new window command. It behaves almost like a promise, causing confusion. Let's replace it with a custom cy.later command.

// if anyone tries to use cy.then in the spec code or in a plugin
// we will get an error
Cypress.Commands.overwrite(
  'then',
  function (then, subject, cb, allowUse) {
    if (!allowUse) {
      throw new Error('Using cy.then command is disallowed')
    }
    return then(subject, cb)
  },
)

Cypress.Commands.add(
  'later',
  { prevSubject: true },
  (subject, cb) => {
    // cy.later behaves just like cy.then
    // which we implement by calling the original cy.then command
    return cy.now('then', subject, cb, true)
  },
)

cy.wrap('Hello')
  .should('be.a', 'string')
  .later((x) => x + x)
  .should('equal', 'HelloHello')

Read the blog post Replace The cy.then Commandopen in new window.