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 中不会出现此错误。相反, 和Cypress都cy被定义为any。
然而,不管这个错误如何,测试都可以正常工作。
我该怎么做才能让 linter 在自己的文件中识别 Cypress?
我找到了一些解决方案。
将其放在每个文件测试脚本的顶部
/// <reference types="cypress" />
Run Code Online (Sandbox Code Playgroud)
yarn add -D eslint-plugin-cypress(https://github.com/cypress-io/eslint-plugin-cypress)
将这些行添加到.eslintrc
/// <reference types="cypress" />
Run Code Online (Sandbox Code Playgroud)
{
"globals": {
"cy": true
}
}
Run Code Online (Sandbox Code Playgroud)
将其添加到.eslintrc
cy 是一个全局变量。很像位置。所以确实是window.cy。您可以将其添加到 Eslint 中的全局变量中。不要从 cypress 导入 cy。