eslint-plugin-promise icon indicating copy to clipboard operation
eslint-plugin-promise copied to clipboard

'promise/catch-or-return' matches incorrectly with Cypress

Open GodOfGrandeur opened this issue 5 years ago • 4 comments
trafficstars

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.

GodOfGrandeur avatar Jan 27 '20 15:01 GodOfGrandeur

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

vadimyen avatar Jul 20 '20 03:07 vadimyen

  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.

xjamundx avatar Jul 20 '20 16:07 xjamundx

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

vadimyen avatar Jul 21 '20 07:07 vadimyen

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"
      }
    },

bmaupin avatar Mar 30 '23 20:03 bmaupin

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 avatar Jul 20 '24 10:07 brettz9