首页 > 解决方案 > 使用 eslintrc 禁用特定文件结尾的 ESlint 规则

问题描述

我正在使用 TypeScript 使用 Jest 和 Cypress 测试我的应用程序。Jest 文件以 . 结尾.test.ts,而 Cypress 测试文件以.spec.ts. 我使用规则附带的Jest ESLint 插件expect-expect。许多赛普拉斯测试不包含expect.

如何为以 结尾的文件禁用此规则.spec.ts?是否可以禁用特定文件扩展名的规则.eslintrc.json?(我想避免在每个.spec.ts文件中写评论。)

编辑:我知道我可以添加"cy.*""assertFunctionNames",但我还需要为.spec文件禁用其他规则,例如valid-expectvalid-expect-in-promise

标签: jestjseslintcypressfile-extensioneslintrc

解决方案


您可以使用文件中的override属性.eslintrc使用配置文件禁用一组文件

所以你想要的是禁用jest/expect-expect所有带有扩展名的文件的规则.spec.ts,你需要在你的.eslintrc文件中添加它。

{
  "rules": {...},
  "overrides": [
    {
      "files": ["*.spec.ts"],
      "rules": {
        "jest/expect-expect": "off"
      }
    }
  ]
}

推荐阅读