【问题标题】:Use ESLint plugin only for some files/directories仅对某些文件/目录使用 ESLint 插件
【发布时间】:2020-05-11 00:08:44
【问题描述】:

我正在使用 ESLint plugin for Jest 对我的 Jest 测试进行 lint。我的项目结构是

my-project
|
|--tests
   |
   |--unit
   |
   |--e2e

我只想在tests/unit 目录中linting 文件时使用Jest 插件(因为tests/e2e 中的文件不是Jest 测试),但是当我运行linting 时,此插件适用于@ 下的所有文件987654325@,这会导致tests/e2e 文件中出现虚假错误。

我可以通过将以下内容添加到eslintrc.js 来禁用tests/e2e 目录中的个别规则

module.exports = {
  root: true,
  env: {
    node: true,
    'jest/globals': true
  },
  extends: [
    'eslint:recommended',
    'plugin:jest/recommended',
  ],
  parserOptions: {
    parser: 'babel-eslint'
  },
  plugins: ['jest'],
  "overrides": [
    {
      "files": ["tests/e2e/*.js"],
      "rules": {
        "jest/no-disabled-tests": "off",
        "jest/expect-expect": "off",
        "jest/valid-expect-in-promise": "off"
      }
    }
  ]
}

但我不想禁用 tests/e2e 目录中的个别规则,而是想完全禁用 Jest 插件。

【问题讨论】:

  • 你最初是如何运行 eslint、git hook、CLI、编辑器插件的?

标签: javascript jestjs eslint


【解决方案1】:

遇到同样的问题,在eslint 的配置文件中没有找到内置的解决方案,但你可以尝试一些对我有用的方法:忽略主eslintrc.js 中的 E2E 测试模式并创建另一个@ 987654323@文件嵌套在E2E目录中。

如下:

my-project
|-- eslintrc.js // this is your main eslint config
|--tests
   |
   |--unit
   |
   |--e2e
      |
      |-- eslintrc.js // nested eslintrc without the "jest" plugin

请注意,您必须告诉嵌套 eslintrc 中的 babel-eslint 解析器 babel 配置文件在哪里(通常作为 parserOptions 的一部分),因为它不再位于同一目录中。

主要eslintrc.js:

module.exports = {
  root: true,
  env: {
    node: true,
    'jest/globals': true
  },
  extends: [
    'eslint:recommended',
    'plugin:jest/recommended',
  ],
  parser: 'babel-eslint'
  plugins: ['jest'],
  ignorePatterns: ["tests/e2e/*.js"]
}

tests/e2e/ 中嵌套eslintrc.js 文件:

module.exports = {
  root: true,
  env: {
    node: true
  },
  extends: ['eslint:recommended'],
  parser: 'babel-eslint',
  parserOptions: {
    babelOptions: {
      configFile: '../../babel.config.js' // assuming this is the name of your babel config file in the project root
    }
  }
}

【讨论】:

    【解决方案2】:

    来自eslint docs 关于覆盖:

    glob 特定配置的工作方式几乎与任何其他 ESLint 配置相同。覆盖块可以包含在常规配置中有效的任何配置选项,root 和 ignorePatterns 除外。

    感谢帮助我的 SO 答案:Eslint allow multiple parsers based on glob patterns

    该答案中的示例还演示了覆盖插件。

    我的示例使用两个覆盖部分,一个用于 JavaScript 文件,一个用于 TypeScript。这很可能是两个不同的目录。

    {
    "overrides": [
      {
        "files": "**/*.ts",
        "plugins": [
          "prettier",
          "@typescript-eslint"
        ],
        "extends": [
          "eslint-config-prettier",
          "prettier",
          "prettier/@typescript-eslint",
          "plugin:import/errors",
          "plugin:import/warnings",
          "plugin:import/typescript",
          "plugin:@typescript-eslint/recommended",
          "plugin:@typescript-eslint/recommended-requiring-type-checking",
          "plugin:prettier/recommended"
        ],
        "parser": "@typescript-eslint/parser",
        "parserOptions": {
          "project": "./tsconfig.json"
        },
        "rules": {
         
        }
      },
      {
        // Disabling TypeScript lint rules for JS files.
        "files": "**/*.js",
        "plugins": [
          "prettier"
        ],
        "extends": [
          "esnext",
          "eslint-config-prettier",
          "prettier",
          "plugin:import/errors",
          "plugin:import/warnings",
          "plugin:prettier/recommended"
        ],
        "parser": "babel-eslint", 
        "parserOptions": {
        },
        "env": {
          "node": true
        },
        "rules": {
          
        }
      }
    ]
    

    }

    【讨论】:

      【解决方案3】:

      您可以在配置文件中使用ignorePatterns 或创建.eslintignore 文件。

      见:https://eslint.org/docs/user-guide/configuring#ignoring-files-and-directories

      【讨论】:

      • 这将完全禁用这些文件的 eslint,这不是 OP 所要求的。他只想为这些文件禁用 Jest 插件
      • 他们可以指定目录tests/e2e 以排除那里的测试,这是他们要求的。
      • 也许我误解了一些东西......根据文档,ignorePatterns 看起来完全禁用了对这些文件运行的 eslint。我认为 OP 仍然希望 eslint 在这些文件上运行,只是不包括 jest 插件。
      • 我的理解是,当他们说“但不是禁用 tests/e2e 目录中的个别规则,我想完全禁用 Jest 插件”时,他们指的是 Jest Eslint 插件。
      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 2017-08-28
      相关资源
      最近更新 更多