How to use ESLint with Jest

后端 未结 9 1703
滥情空心
滥情空心 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条回答
  •  Happy的楠姐
    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

提交回复
热议问题