ChatGPT解决这个技术问题 Extra ChatGPT

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?


A
Avinash Singh

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.


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.
T
Trapenok Victor

Add following comment line in head of your file

/* globals describe, expect, it */ 

a
avalanche1

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


Thanks! this is what worked for me...
A
Atuhaire Collins

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