Skip to content

fix: Adding an existing command with Cypress.Commands.add() will throw an error #18587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
200980f
throw when adding an existing command
davidmunechika Oct 21, 2021
13f38f6
add test
davidmunechika Oct 21, 2021
57e66f8
fix test
davidmunechika Oct 21, 2021
b3f90bf
fix tests
davidmunechika Oct 21, 2021
4cb79e3
fix tests
davidmunechika Oct 21, 2021
79b9ea6
remove tests for user defined commands
davidmunechika Oct 25, 2021
3c7eb7d
only throw for core commands
davidmunechika Oct 26, 2021
e545ebc
fix beforeEach
davidmunechika Oct 26, 2021
7e34776
Merge branch 'develop' into issue-18572-add-existing-command
davidmunechika Oct 26, 2021
0d30ba2
change to obj
davidmunechika Oct 26, 2021
4c23b57
Merge branch 'issue-18572-add-existing-command' of https://github.com…
davidmunechika Oct 26, 2021
240be6a
Update packages/driver/src/cypress/commands.ts
davidmunechika Oct 26, 2021
027e635
fix tests
davidmunechika Oct 26, 2021
53fc0ac
revert change
davidmunechika Oct 26, 2021
0ee7b13
Merge branch 'develop' into issue-18572-add-existing-command
davidmunechika Oct 26, 2021
88255a7
change scope of var
davidmunechika Oct 27, 2021
e81bde5
Merge branch 'issue-18572-add-existing-command' of https://github.com…
davidmunechika Oct 27, 2021
eeb96dc
Merge branch 'develop' into issue-18572-add-existing-command
davidmunechika Oct 27, 2021
910f936
Merge branch 'develop' into issue-18572-add-existing-command
davidmunechika Oct 28, 2021
669d364
change documentation link
davidmunechika Oct 29, 2021
6cf39e3
change documentation link
davidmunechika Oct 29, 2021
6fb70fb
Merge branch 'develop' into issue-18572-add-existing-command
davidmunechika Nov 1, 2021
ab1fc61
Merge branch 'develop' into issue-18572-add-existing-command
davidmunechika Nov 5, 2021
458d142
improve stack trace
davidmunechika Nov 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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