cypress-io / eslint-plugin-cypress

An ESLint plugin for projects that use Cypress

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cy.focused().type("text") shows cypress/unsafe-to-chain-command error

BCaspari opened this issue ยท comments

After an update to 2.14.0, the cypress/unsafe-to-chain-command rule is triggered by code as
cy.focused().type("text").
This should be no problem however, as the cypress docu states cy.focused() is a query, and it is safe to chain further commands (https://docs.cypress.io/api/commands/focused)

commented

Hi @BCaspari , you are correct that cy.focused is a query and safe to chain off of. cy.focus is however not safe to chain off of. I believe this might be an overzealous match from this PR. Thank you for reporting this issue. I am not sure when we will be able to address this issue, so for the time being I would suggest downgrading and pinning your version to avoid this problem.

This would also impact cypress-io/cypress-example-kitchensink if it weren't for the fact that unsafe-to-chain linting is currently disabled in that repo.

Even doing cy.focused().blur(), which is an example given in the docs, raises this error now.

@nagash77

Since v2.14.0 has introduced an unplanned breaking change, if this issue can't be fixed in a timely way, then it should be considered to revert the PR #137 and release a corrected version without the breaking change.

commented

I believe this to be an unintended side effect from this change specifically:

49c6eb3#diff-43276831483080b5e9f5d83eb749bebf8e0d7167aa50c64cbc4fc1bcd7e0698bR121

Which comes from here:

49c6eb3#diff-43276831483080b5e9f5d83eb749bebf8e0d7167aa50c64cbc4fc1bcd7e0698bR111-R115

Seems like before this change, any selector had to exactly match the name of the "unsafe to chain" selectors. Now, because of the regex, if the selector's name partially matches one of the names, the linter will complain.

e.g. /focus/.test("focused") returns true, so the linter complains.

Any progress on this one? Is there anything I could do to help? The issue and the cause seems to be identified.

I've made a PR to illustrate the false positive, and a potential fix, here: #142

@dominicfraser

  • Since your PR #142 has been merged, can this issue now be closed?

I believe so yes โ˜บ๏ธ

A more scalable solution is proposed in #141 (rather than the fast hotfix I did) - but for this specific bug the merged PR and new release solves it adequately ๐Ÿ‘

the implementation is wrong.. Not only focused but every command that contains any of the unsafe to chain commands shows cypress/unsafe-to-chain-command error.
i.e. custom command submitBtn shows cypress/unsafe-to-chain-command error.

Please check #141 because we have to rename our functions or disable linting and that's not the proper solution..

  • This issue is resolved by PR #141

๐ŸŽ‰ This issue has been resolved in version 3.1.1 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€