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

Ron*_*nny 5 javascript webstorm eslint cypress

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

命令.js

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

// ESLint: 'Cypress' is not defined.(no-undef)
// ESLint: 'cy' is not defined.(no-undef)
Run Code Online (Sandbox Code Playgroud)

索引.js

import './commands.js'
Run Code Online (Sandbox Code Playgroud)

VSCode 中不会出现此错误。相反, 和Cypresscy被定义为any

然而,不管这个错误如何,测试都可以正常工作。

我该怎么做才能让 linter 在自己的文件中识别 Cypress?

Lew*_*ran 8

我找到了一些解决方案。

添加参考

将其放在每个文件测试脚本的顶部

/// <reference types="cypress" />
Run Code Online (Sandbox Code Playgroud)

Eslint 扩展

/// <reference types="cypress" />
Run Code Online (Sandbox Code Playgroud)

在 eslintrc 全局变量中指定 cy

{
  "globals": {
    "cy": true
  }
}
Run Code Online (Sandbox Code Playgroud)

将其添加到.eslintrc

cy 是一个全局变量。很像位置。所以确实是window.cy。您可以将其添加到 Eslint 中的全局变量中。不要从 cypress 导入 cy。

参考:ESLint:'cy'未定义(赛普拉斯)