How to use ESLint with Jest?

Sometimes, we want to use ESLint with Jest.

In this article, we’ll look at how to use ESLint with Jest.

How to use ESLint with Jest?

To use ESLint with Jest, we add some entries into .eslintrc.

We add

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

into .eslintrc.

And we may need plugins: ["jest"] in out ESLint config.

We can add rule overrides with the rules section as we have below.

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

Conclusion

To use ESLint with Jest, we add some entries into .eslintrc.