Skip to content
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

avoid 'promise/catch-or-return' errors with Cypress calls #180

Closed
GodOfGrandeur opened this issue Jan 27, 2020 · 5 comments · Fixed by #495
Closed

avoid 'promise/catch-or-return' errors with Cypress calls #180

GodOfGrandeur opened this issue Jan 27, 2020 · 5 comments · Fixed by #495

Comments

@GodOfGrandeur
Copy link

GodOfGrandeur commented Jan 27, 2020

Description

When using the rule promise/catch-or-return you get a error with Cypress because they have a then method.

https://docs.cypress.io/api/commands/then.html#Syntax

Steps to Reproduce

  1. Create repo with this module and Cypress installed
  2. Create integration file where you make use of then method. (see docs)

Expected behavior:
Need a way to exclude this since this isn't actually a promise.

Actual behavior:
This line is marked as needing a catch added but this isn't a promise.

Versions

  • Node version: v10.16.2
  • ESLint version: v5.3.0
  • eslint-plugin-promise version: v4.2.1

Additional Information

Not sure why the library thinks this is a promise but it shouldn't.

@vadimyen
Copy link

vadimyen commented Jul 20, 2020

+1, I got the same trouble, so I manually add specific exceptions to overrides block

@xjamundx
Copy link
Contributor

xjamundx commented Jul 20, 2020

  1. The reason it gets confused is because eslint doesn't run the code or have any idea on the actual types, it has to use heuristics to guess and the best one to use is if there's a method called .then()

  2. The Cypress docs say .then() is modeled identically to the way Promises work in JavaScript.

  3. Eslint already provides a way to disable a rule per file using /* eslint-disable promise/catch-or-return*/

  4. You can also disable per-folder (such as your testing folder) with a custom eslintrc.js file

That all being said....is the parent variable always called something like cy. If that's the case it would be possible to exclude it.

@vadimyen
Copy link

vadimyen commented Jul 21, 2020

AFAIK and according to the docs, there's always the top-level global variable named cy in Cypress method chaining

@bmaupin
Copy link

bmaupin commented Mar 30, 2023

The promise/always-return is also incorrectly matched. I added rules overrides in my eslint config to disable them just for Cypress, e.g.:

  "overrides": [
    {
      "extends": ["plugin:cypress/recommended"],
      "files": ["cypress.config.ts", "cypress/**/*.ts"],
      "parserOptions": {
        "project": ["cypress/tsconfig.json"]
      },
      "rules": {
        "promise/always-return": "off",
        "promise/catch-or-return": "off"
      }
    },

@brettz9
Copy link
Member

brettz9 commented Jul 20, 2024

The same problem occurs if prefer-await-to-then is enabled. Detecting cy would be a good heuristic, but not fully reliable if one is using utilities to call cy. Still, might be helpful to avoid the need for overrides.

@brettz9 brettz9 changed the title 'promise/catch-or-return' matches incorrectly with Cypress avoid 'promise/catch-or-return' errors with Cypress calls Jul 20, 2024
brettz9 added a commit to brettz9/eslint-plugin-promise that referenced this issue Jul 22, 2024
ota-meshi pushed a commit that referenced this issue Jul 24, 2024
…s commands (#495)

Fixes #180

**What is the purpose of this pull request?**

- [x] Changes an existing rule

**What changes did you make? (Give an overview)**

Do not report Cypress commands (e.g., `cy.get('sel').then()`) for
`catch-or-return` and `prefer-await-to-then`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants