描述在安装时没有定义

我在我的项目中安装了 jest v24.7.1:

npm install jest -D

然后我开始写一些测试文件,但是我得到了这些 eslint 错误:

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

返回文章页面

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: {}
};

我应该添加另一个规则或插件来解决这个问题吗?

36153 次浏览

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.

Add following comment line in head of your file

/* globals describe, expect, it */

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

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