describe is not defined when installing jest
Asked Answered
H

4

114

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?

Huskamp answered 23/4, 2019 at 9:8 Comment(1)
Does this answer your question? eslint throws `no-undef` errors when linting Jest test filesInnings
R
281

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.

Renayrenckens answered 23/4, 2019 at 9:36 Comment(2)
If you are using VS-code you might need to restart the Es-lint server by pressing ctrl/command + shift + p, then in the command interpreter search for Restart Eslint and click to continue the eslint server.Vernacularize
Wanted to add that jest also uses some jasmine defines like fail so it may be necessary to add jasmine: true alsoDara
H
7

Add following comment line in head of your file

/* globals describe, expect, it */ 
Hanger answered 3/9, 2020 at 2:5 Comment(0)
I
1

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

Inorganic answered 13/12, 2021 at 15:0 Comment(0)
C
-7

Add /* eslint-disable no-undef */ to the top of your test files.

sample.test.js

/* eslint-disable no-undef */
function sum(a, b) {
  return a + b;
}

describe("adds 1 + 2 to equal 3", () => {
  expect(sum(1, 2)).toBe(3);
});

Cylindroid answered 7/6, 2022 at 7:50 Comment(1)
This is not correct - you shouldn't just hide the error, adding jest to the environment within eslint fixed this for me.Nineveh

© 2022 - 2024 — McMap. All rights reserved.