describe is not defined when installing jest

JestjsEslintEslint Config-AirbnbEslintrc

Jestjs Problem Overview


I installed jest v24.7.1in my project with:

npm install jest -D

Then I start writing some test files, However I got these eslint errors:

'describe' is not defined. eslint (no-undef)
'it' is not defined. eslint (no-undef)
'expect' is not defined. eslint (no-undef)

eslintrc.js:

module.exports = {


env: {
    browser: true,
    es6: true
  },
  extends: ["airbnb", "prettier", "prettier/react"],
  globals: {
    Atomics: "readonly",
    SharedArrayBuffer: "readonly"
  },
  parserOptions: {
    ecmaFeatures: {
      jsx: true
    },
    ecmaVersion: 2018,
    sourceType: "module"
  },
  plugins: ["react"],
  rules: {}
};

Should I add another rule or a plugin to fix this?

Jestjs Solutions


Solution 1 - Jestjs

Add following line in .eslintrc.js file

"env": {
    "jest": true
}

or

{
  "plugins": ["jest"]
},
"env": {
  "jest/globals": true
}

For more details check here, it also define the same.

Hope you installed eslint-plugin-jest package.If not kindly go through for Documentation.

All the configuration details of Configuring ESLint.

Solution 2 - Jestjs

Add following comment line in head of your file

/* globals describe, expect, it */ 

Solution 3 - Jestjs

For jest 27 all of this should not be necessary.
Kill node_modules & package-lock.json.
Then run npm i.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSlimView Question on Stackoverflow
Solution 1 - JestjsAvinash SinghView Answer on Stackoverflow
Solution 2 - JestjsTrapenok VictorView Answer on Stackoverflow
Solution 3 - Jestjsavalanche1View Answer on Stackoverflow