Categories
JavaScript Answers

How to use ESLint with Jest?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *