104

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?

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Slim
  • 5,527
  • 13
  • 45
  • 81

4 Answers4

262

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.

Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
  • 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. – Mwibutsa Floribert Jun 23 '22 at 17:41
  • Wanted to add that jest also uses some jasmine defines like `fail` so it may be necessary to add `jasmine: true` also – rcbevans Aug 29 '22 at 18:10
5

Add following comment line in head of your file

/* globals describe, expect, it */ 
Trapenok Victor
  • 349
  • 2
  • 6
1

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

avalanche1
  • 3,154
  • 1
  • 31
  • 38
-4

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);
});