Cypress 9.0.0 will throw an error on adding existing commands

Cypress gives us the ability to create custom commands and overwrite the existing commands. It comes with its API for creating custom and overwriting existing commands.

Syntax

Cypress.Commands.add(name, options, callbackFn)

Usage

Cypress.Commands.add('login', (email, password) => {})

Overwriting existing commands

Before version 9.0.0, Cypress did not warn us when we try to add a command that already exists in cypress using Cypress.Commands.add() and it leads to the unexpected behavior with a command since it was unclear which would take precedence.

From Cypress 9.0.0, if we try to add a command that already exists using Cypress.Commands.add(), Cypress will throw an error indicating that Cypress.Commands.overwrite() should be used instead to overwrite the behavior of existing commands.

Cypress.Commands.add('get', () => {
  cy.get('button').first()
})

// Error: `Cypress.Commands.add()` is used to create new commands,
but `get` is an existing Cypress command.
Please use `Cypress.Commands.overwrite()`
if we would like to overwrite an existing command.

This change is a breaking one, and it will throw an error if we had previously added any new command which already existed in Cypress.

Note:

It only works with the core Cypress commands. If we try to add a duplicate custom command that we have already defined, then Cypress will not throw an error. If we define more than one custom command with the same name Cypress will execute the last defined command.

Need help on your Ruby on Rails or React project?

Join Our Newsletter