Skip to content

Commit

Permalink
fix: Adding an existing command with Cypress.Commands.add() will th…
Browse files Browse the repository at this point in the history
…row an error (#18587)

Co-authored-by: Chris Breiding <chrisbreiding@users.noreply.github.com>
Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
  • Loading branch information
3 people committed Nov 8, 2021
1 parent fc83e64 commit b735978
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/driver/cypress/integration/commands/commands_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ describe('src/cy/commands/commands', () => {
expect($ce.get(0)).to.eq(ce.get(0))
})
})

it('throws when attempting to add an existing command', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.eq('`Cypress.Commands.add()` is used to create new commands, but `get` is an existing Cypress command.\n\nPlease use `Cypress.Commands.overwrite()` if you would like to overwrite an existing command.\n')
expect(err.docsUrl).to.eq('https://on.cypress.io/custom-commands')

done()
})

Cypress.Commands.add('get', () => {
cy
.get('[contenteditable]')
.first()
})
})
})

context('errors', () => {
Expand Down
25 changes: 25 additions & 0 deletions packages/driver/src/cypress/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import _ from 'lodash'

import $errUtils from './error_utils'
import $stackUtils from './stack_utils'

import { allCommands } from '../cy/commands'
import { addCommand } from '../cy/net-stubbing'
Expand Down Expand Up @@ -30,6 +31,10 @@ export default {
// of commands
const commands = {}
const commandBackups = {}
// we track built in commands to ensure users cannot
// add custom commands with the same name
const builtInCommandNames = {}
let addingBuiltIns

const store = (obj) => {
commands[obj.name] = obj
Expand Down Expand Up @@ -126,6 +131,24 @@ export default {
},

add (name, options, fn) {
if (builtInCommandNames[name]) {
$errUtils.throwErrByPath('miscellaneous.invalid_new_command', {
args: {
name,
},
stack: (new state('specWindow').Error('add command stack')).stack,
errProps: {
appendToStack: {
title: 'From Cypress Internals',
content: $stackUtils.stackWithoutMessage((new Error('add command internal stack')).stack),
} },
})
}

if (addingBuiltIns) {
builtInCommandNames[name] = true
}

if (_.isFunction(options)) {
fn = options
options = {}
Expand Down Expand Up @@ -163,12 +186,14 @@ export default {
},
}

addingBuiltIns = true
// perf loop
for (let cmd of builtInCommands) {
// support "export default" syntax
cmd = cmd.default || cmd
cmd(Commands, Cypress, cy, state, config)
}
addingBuiltIns = false

return Commands
},
Expand Down
4 changes: 4 additions & 0 deletions packages/driver/src/cypress/error_messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,10 @@ export default {
message: 'Could not find a command for: `{{name}}`.\n\nAvailable commands are: {{cmds}}.\n',
docsUrl: 'https://on.cypress.io/api',
},
invalid_new_command: {
message: '`Cypress.Commands.add()` is used to create new commands, but `{{name}}` is an existing Cypress command.\n\nPlease use `Cypress.Commands.overwrite()` if you would like to overwrite an existing command.\n',
docsUrl: 'https://on.cypress.io/custom-commands',
},
invalid_overwrite: {
message: 'Cannot overwite command for: `{{name}}`. An existing command does not exist by that name.',
docsUrl: 'https://on.cypress.io/api',
Expand Down
6 changes: 4 additions & 2 deletions packages/driver/src/cypress/error_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,10 @@ const throwErr = (err, options = {}) => {
const throwErrByPath = (errPath, options = {}) => {
const err = errByPath(errPath, options.args)

// gets rid of internal stack lines that just build the error
if (Error.captureStackTrace) {
if (options.stack) {
err.stack = $stackUtils.replacedStack(err, options.stack)
} else if (Error.captureStackTrace) {
// gets rid of internal stack lines that just build the error
Error.captureStackTrace(err, throwErrByPath)
}

Expand Down

2 comments on commit b735978

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b735978 Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.8.0/circle-9.0-release-b735978315ad279823cd7532b882643405e95973/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b735978 Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/8.8.0/circle-9.0-release-b735978315ad279823cd7532b882643405e95973/cypress.tgz

Please sign in to comment.