Skip to content

fix: Cannot add commands with "reserved" names #18923

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

Conversation

davidmunechika
Copy link
Contributor

@davidmunechika davidmunechika commented Nov 15, 2021

User facing changelog

Attempting to add a new command using Cypress.Commands.add() that has the same name as an internal function reserved by Cypress will show an improved error message explaining why the command name must be changed.

Additional details

Previously users would encounter strange behavior when trying to add new custom commands with the same name as a reserved internal function by Cypress (i.e. TypeError...). This change improves the error message to explain that users cannot name new custom commands as the same name as an internal function already reserved by Cypress.

Old behavior:

Screenshot 2021-11-17 at 8 42 11 AM

New error and stacktrace:

image

PR Tasks

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Nov 15, 2021

Thanks for taking the time to open a PR!

@davidmunechika davidmunechika self-assigned this Nov 15, 2021
@cypress
Copy link

cypress bot commented Nov 15, 2021



Test summary

18149 0 202 0Flakiness 1


Run details

Project cypress
Status Passed
Commit 5502541
Started Dec 2, 2021 2:18 PM
Ended Dec 2, 2021 2:30 PM
Duration 11:54 💡
OS Linux Debian - 10.10
Browser Multiple

View run in Cypress Dashboard ➡️


Flakiness

cypress/integration/cypress/proxy-logging-spec.ts Flakiness
1 Proxy Logging > request logging > xhr log has response body/status code

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@davidmunechika davidmunechika marked this pull request as ready for review November 16, 2021 14:07
@davidmunechika davidmunechika requested a review from a team as a code owner November 16, 2021 14:07
@davidmunechika davidmunechika requested review from jennifer-shehane and tbiethman and removed request for a team November 16, 2021 14:07
@davidmunechika davidmunechika changed the title fix: Cannot add commands with "reserved" names: getAlias, reset fix: Cannot add commands with "reserved" names Nov 17, 2021
@chrisbreiding chrisbreiding self-requested a review November 17, 2021 16:01
@emilyrohrbough
Copy link
Member

@davidmunechika Do you have a screenshot of the new error & stack trace?

@davidmunechika
Copy link
Contributor Author

@emilyrohrbough
Screenshot 2021-11-18 at 9 35 22 AM

Copy link
Contributor

@chrisbreiding chrisbreiding left a comment

Choose a reason for hiding this comment

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

Here's a way to reproduce an issue with cy.reset:

Cypress.Commands.add('reset', () => {})

it('a test', () => {})

Screen Shot 2021-11-19 at 9 41 27 AM

It errors even though the user hasn't called the cy.reset() command they created, because we use it internally.

We need to do this for every method/property we add internally to cy. If they "add" a command with that name, it's overriding the functionality of that method and can cause issues. It might take a certain set of circumstances to reproduce an issue, but those methods are relied on somewhere in the codebase, so we can't allow them to be overwritten.

Really, we shouldn't have so many utility methods attached to cy. I think most of them are on there for convenience's sake. I would say we shouldn't have any undocumented methods on cy. At least they should be prefixed with an underscore or under a namespace that makes it clear they're for internal use. Something that reduces the number of unusable command names. Even better would be to refactor them out into more appropriate places and reduce the need for a massive object of utility methods. But this should be a separate discussion and effort.

For now and for this PR, I think it makes sense to add all these methods/properties to the list of reserved names and error if a user tries to use them. In the current state of the world, they'll cause unexpected behavior if overridden. Then if we refactor them off of cy in the future, they can be removed from the list of reserved names.

@jennifer-shehane jennifer-shehane added the type: breaking change Requires a new major release version label Nov 22, 2021
BlueWinds
BlueWinds previously approved these changes Nov 22, 2021
Co-authored-by: Chris Breiding <[email protected]>
chrisbreiding
chrisbreiding previously approved these changes Nov 23, 2021
@chrisbreiding chrisbreiding changed the base branch from develop to 10.0-release November 23, 2021 17:11
@chrisbreiding chrisbreiding dismissed their stale review November 23, 2021 17:11

The base branch was changed.

@chrisbreiding
Copy link
Contributor

I changed the base branch to 10.0-release, but I'm sure how well that will resolve. It's possible it would be easier to create a new branch based off 10.0-release and cherry-pick the changes into it.

@CLAassistant
Copy link

CLAassistant commented Dec 1, 2021

CLA assistant check
All committers have signed the CLA.

@jennifer-shehane jennifer-shehane merged commit 28b992f into 10.0-release Dec 2, 2021
@jennifer-shehane jennifer-shehane deleted the issue-6146-prevent-adding-reserved-commands branch December 2, 2021 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: breaking change Requires a new major release version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot add commands with "reserved" names: getAlias, reset
6 participants