Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

describe is not defined when installing jest

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?

like image 391
Slim Avatar asked Apr 23 '19 09:04

Slim


People also ask

How do I import jest?

The jest object is automatically in scope within every test file. The methods in the jest object help create mocks and let you control Jest's overall behavior. It can also be imported explicitly by via import {jest} from '@jest/globals' .

Does jest require node?

To read TypeScript configuration files Jest requires ts-node . Make sure it is installed in your project.


3 Answers

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.

like image 131
Avinash Singh Avatar answered Oct 05 '22 15:10

Avinash Singh


Add following comment line in head of your file

/* globals describe, expect, it */ 
like image 22
Trapenok Victor Avatar answered Oct 05 '22 15:10

Trapenok Victor


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

like image 33
avalanche1 Avatar answered Oct 05 '22 15:10

avalanche1