How to use ESLint with Jest

后端 未结 9 1667
滥情空心
滥情空心 2020-12-07 11:56

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

Jest tests run with some globals like jest, which I\'ll need to tell the lin

相关标签:
9条回答
  • 2020-12-07 12:12

    In your .eslintignore file add the following value:

    **/__tests__/
    

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

    0 讨论(0)
  • 2020-12-07 12:16

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

    /* eslint-env jest */
    
    describe(() => {
      /* ... */
    })
    
    0 讨论(0)
  • 2020-12-07 12:26

    The docs show you are now able to add:

    "env": {
        "jest/globals": true
    }
    

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

    0 讨论(0)
  • 2020-12-07 12:28

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

      "globals": {
        "jest": true,
      }
    
    0 讨论(0)
  • 2020-12-07 12:29

    ESLint supports this as of version >= 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 :

    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

    0 讨论(0)
  • 2020-12-07 12:30

    Add environment only for __tests__ folder

    You could add a .eslintrc.yml file in your __tests__ folders, that extends you basic configuration:

    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.

    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:

    #!/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.yml file with to configuration shown above. This script has to be launched within the folder containing your parent .eslintrc.

    0 讨论(0)
提交回复
热议问题