我试图使用ESLint linter与Jest测试框架。

Jest测试与一些全局变量(如Jest)一起运行,我需要告诉linter;但棘手的是目录结构,在Jest中,测试与源代码一起嵌入在__tests__文件夹中,因此目录结构看起来像这样:

src
    foo
        foo.js
        __tests__
            fooTest.js
    bar
        bar.js
        __tests__
            barTest.js

通常,我将所有的测试都放在一个目录下,我可以在那里添加一个.eslintrc文件来添加全局变量…但我当然不想在每个__test__目录下都添加一个.eslintrc文件。

目前,我只是将测试全局变量添加到全局的.eslintrc文件中,但由于这意味着我现在可以在非测试代码中引用笑话,这似乎不是“正确”的解决方案。

是否有一种方法让eslint应用基于目录名或类似内容的模式的规则?


当前回答

ESLint从>= 4版本开始支持:

/*
.eslintrc.js
*/
const ERROR = 2;
const WARN = 1;

module.exports = {
  extends: "eslint:recommended",
  env: {
    es6: true
  },
  overrides: [
    {
      files: [
        "**/*.test.js"
      ],
      env: {
        jest: true // now **/*.test.js files' env has both es6 *and* jest
      },
      // Can't extend in overrides: https://github.com/eslint/eslint/issues/8813
      // "extends": ["plugin:jest/recommended"]
      plugins: ["jest"],
      rules: {
        "jest/no-disabled-tests": "warn",
        "jest/no-focused-tests": "error",
        "jest/no-identical-title": "error",
        "jest/prefer-to-have-length": "warn",
        "jest/valid-expect": "error"
      }
    }
  ],
};

这里有一个变通方法(从这里的另一个答案,投票!)对于eslint配置的“扩展覆盖”限制:

overrides: [
  Object.assign(
    {
      files: [ '**/*.test.js' ],
      env: { jest: true },
      plugins: [ 'jest' ],
    },
    require('eslint-plugin-jest').configs.recommended
  )
]

从https://github.com/eslint/eslint/issues/8813 # issuecomment - 320448724

其他回答

您还可以在测试文件中设置测试环境,如下所示:

/* eslint-env jest */

describe(() => {
  /* ... */
})

ESLint从>= 4版本开始支持:

/*
.eslintrc.js
*/
const ERROR = 2;
const WARN = 1;

module.exports = {
  extends: "eslint:recommended",
  env: {
    es6: true
  },
  overrides: [
    {
      files: [
        "**/*.test.js"
      ],
      env: {
        jest: true // now **/*.test.js files' env has both es6 *and* jest
      },
      // Can't extend in overrides: https://github.com/eslint/eslint/issues/8813
      // "extends": ["plugin:jest/recommended"]
      plugins: ["jest"],
      rules: {
        "jest/no-disabled-tests": "warn",
        "jest/no-focused-tests": "error",
        "jest/no-identical-title": "error",
        "jest/prefer-to-have-length": "warn",
        "jest/valid-expect": "error"
      }
    }
  ],
};

这里有一个变通方法(从这里的另一个答案,投票!)对于eslint配置的“扩展覆盖”限制:

overrides: [
  Object.assign(
    {
      files: [ '**/*.test.js' ],
      env: { jest: true },
      plugins: [ 'jest' ],
    },
    require('eslint-plugin-jest').configs.recommended
  )
]

从https://github.com/eslint/eslint/issues/8813 # issuecomment - 320448724

从ESLint V 6(2019年底发布)开始,你可以在基于glob的配置中使用extends,如下所示:

    "overrides": [
      {
        "files": ["*.test.js"],
        "env": {
          "jest": true
        },
        "plugins": ["jest"],
        "extends": ["plugin:jest/recommended"]
      }
    ]

一些答案假设你已经安装了eslint-plugin-jest,但是不需要这样做,你可以简单地在你的.eslintrc文件中这样做,添加:

  "globals": {
    "jest": true,
  }

在花了一段时间尝试不同的选择后,我让它运行起来了。希望这能帮助其他陷入困境的人。

.eslintrc.json(在项目根文件夹中):

{
    "env": {
        "browser": true,
        "es2021": true,
        "jest/globals": true
    },
    "extends": [
        "standard",
        "plugin:jest/all"
    ],
    "parser": "@babel/eslint-parser",
    "parserOptions": {
        "ecmaVersion": 12,
        "sourceType": "module"
    },
    "rules": {
        "jest/no-hooks": [
            "error",
            {
                "allow": [
                    "afterEach",
                    "beforeEach"
                ]
            }
        ]
    },
    "plugins": [
        "jest"
    ]
}

空的.babelrc(在根项目文件夹中):

{}

.package。Json(在项目根文件夹):

{
  "scripts": {
    "test": "jest",
    "lint": "npx eslint --format=table .",
    "lintfix": "npx eslint --fix ."
  },
  "devDependencies": {
    "@babel/core": "^7.15.0",
    "@babel/eslint-parser": "^7.15.0",
    "aws-sdk-mock": "^5.2.1",
    "eslint": "^7.32.0",
    "eslint-config-standard": "^16.0.3",
    "eslint-plugin-import": "^2.24.0",
    "eslint-plugin-jest": "^24.4.0",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^5.1.0",
    "jest": "^27.0.6"
  }
}

VS Code settings.xml(编辑器配置:启用自动修复保存+ babel解析器):

    "eslint.alwaysShowStatus": true,
    "eslint.format.enable": true,
    "eslint.lintTask.enable": true,
    "eslint.options": {
        "parser": "@babel/eslint-parser"
    },
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "eslint.validate": [
        "javascript"
    ]