Javascript 如何在 Jest 中使用 ESLint

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31629389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:56:18  来源:igfitidea点击:

How to use ESLint with Jest

javascriptjestjseslint

提问by Retsam

I'm attempting to use the ESLint linter with the Jest testing framework.

我正在尝试将 ESLint linter 与 Jest 测试框架一起使用。

Jest tests run with some globals like jest, which I'll need to tell the linter about; but the tricky thing is the directory structure, with Jest the tests are embedded with the source code in __tests__folders, so the directory structure looks something like:

Jest 测试使用一些全局变量,例如jest,我需要告诉 linter;但棘手的是目录结构,在 Jest 中,测试与__tests__文件夹中的源代码一起嵌入,因此目录结构如下所示:

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

Normally, I'd have all my tests under a single dir, and I could just add an .eslintrcfile there to add the globals... but I certainly don't want to add a .eslintrcfile to every single __test__dir.

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

For now, I've just added the test globals to the global .eslintrcfile, but since that means I could now reference jestin non-testing code, that doesn't seem like the "right" solution.

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

Is there a way to get eslint to apply rules based on some pattern based on the directory name, or something like that?

有没有办法让 eslint 应用基于目录名称的某种模式的规则,或者类似的东西?

回答by Dave Cooper

The docsshow you are now able to add:

文档显示您现在可以添加:

"env": {
    "jest": true
}

To your .eslintrcwhich will add all the jest related things to your environment, eliminating the linter errors/warnings.

为了您.eslintrc将所有的笑话相关的东西添加到您的环境,消除了棉短绒的错误/警告。

回答by Zachary Ryan Smith

ESLint supports this as of version >= 4:

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"
      }
    }
  ],
};

Here is a workaround (from another answer on here, vote it up!) for the "extend in overrides" limitation of eslint config :

这是针对 eslint config 的“扩展覆盖”限制的解决方法(来自此处的另一个答案,请投票!):

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

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

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

回答by HaNdTriX

You can also set the test env in your test file as follows:

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

/* eslint-env jest */

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

回答by Ricovitch

To complete Zachary's answer, here is a workaround for the "extend in overrides" limitation of eslint config :

要完成 Zachary 的回答,这里是 eslint config 的“扩展覆盖”限制的解决方法:

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

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

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

回答by Ilya Volodin

Pattern based configs are scheduled for 2.0.0 release of ESLint. For now, however, you will have to create two separate tasks (as mentioned in the comments). One for tests and one for the rest of the code and run both of them, while providing different .eslintrc files.

基于模式的配置计划用于 ESLint 2.0.0 版本。但是,就目前而言,您必须创建两个单独的任务(如评论中所述)。一个用于测试,一个用于其余代码并运行它们,同时提供不同的 .eslintrc 文件。

P.S. There's a jest environment coming in the next release of ESLint, it will register all of the necessary globals.

PS 在 ESLint 的下一个版本中会有一个 jest 环境,它将注册所有必要的全局变量。

回答by Brance Lee

I solved the problem REF

我解决了问题REF

Run

# For Yarn
yarn add eslint-plugin-jest -D

# For NPM
npm i eslint-plugin-jest -D

And then add in your .eslintrcfile

然后在你的.eslintrc文件中添加

{
    "extends": ["airbnb","plugin:jest/recommended"],
}

回答by Sebastian Scholle

some of the answers assume you have 'eslint-plugin-jest' installed, however without needing to do that, you can simply do this in your .eslintrcfile, add:

一些答案假设您安装了“eslint-plugin-jest”,但是无需这样做,您只需在.eslintrc文件中执行此操作,添加:

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

回答by Ulysse BN

Add environment only for __tests__folder

仅为__tests__文件夹添加环境

You could add a .eslintrc.ymlfile in your __tests__folders, that extends you basic configuration:

您可以.eslintrc.yml__tests__文件夹中添加一个文件,扩展您的基本配置:

extends: <relative_path to .eslintrc>
env:
    jest: true

If you have only one __tests__folder, this solution is the best since it scope jest environment only where it is needed.

如果您只有一个__tests__文件夹,则此解决方案是最好的,因为它仅将 jest 环境范围限定在需要的地方。

Dealing with many test folders

处理许多测试文件夹

If you have more test folders (OPs case), I'd still suggest to add those files. And if you have tons of those folders can add them with a simple zsh script:

如果您有更多测试文件夹(OP 案例),我仍然建议添加这些文件。如果您有大量这些文件夹,可以使用简单的 zsh 脚本添加它们:

#!/usr/bin/env zsh

for folder in **/__tests__/ ;do
    count=$(($(tr -cd '/' <<< $folder | wc -c)))
    echo $folder : $count
    cat <<EOF > $folder.eslintrc.yml
extends: $(printf '../%.0s' {1..$count}).eslintrc
env:
    jest: true
EOF
done

This script will look for __tests__folders and add a .eslintrc.ymlfile with to configuration shown above. This script has to be launched within the folder containing your parent .eslintrc.

此脚本将查找__tests__文件夹并将.eslintrc.yml文件添加到上面显示的配置中。此脚本必须在包含您的 parent 的文件夹中启动.eslintrc

回答by Jason Shimkoski

In your .eslintignore file add the following value:

在您的 .eslintignore 文件中添加以下值:

**/__tests__/

This should ignore all instances of the __tests__ directory and their children.

这应该忽略 __tests__ 目录及其子目录的所有实例。