commands.js 中未定义 Cypress 和 cy,但一切正常

Cypress and cy undefined in commands.js, but everything works

当在 commands.js 中键入自定义命令时,WebStorm 的 linter 说 Cypresscy 都未定义并且不提供任何 IntelliSense。 两者都在任何 integration 文件中完美定义。

commands.js

Cypress.Commands.add('command', () => {
  cy.get('div');
});

// ESLint: 'Cypress' is not defined.(no-undef)
// ESLint: 'cy' is not defined.(no-undef)

index.js

import './commands.js'

此错误未出现在 VSCode 中。 Cypresscy 都定义为 any.

然而,不管这个错误,测试工作正常。

如何让 linter 在其自己的文件中识别 Cypress?

我找到了一些解决方案。

添加引用

把它放在每个文件测试脚本的顶部

/// <reference types="cypress" />

Eslint 扩展

{
  "extends": [
    "plugin:cypress/recommended"
  ]
}

在 eslintrc 全局变量中指定 cy

{
  "globals": {
    "cy": true
  }
}

将此添加到 .eslintrc

cy is a global variable. Much like location. So really it is window.cy. You can add it to the globals in Eslint. Don't import cy from cypress.

参考:ESLint: 'cy' is not defined (Cypress)