关于jestjs:describe在安装jest时未定义

describe is not defined when installing jest

我在项目中使用以下命令安装了jest v24.7.1

1
npm install jest -D

然后,我开始编写一些测试文件,但是出现了以下错误提示:

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

eslintrc.js:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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: {}
};

我应该添加其他规则或插件来解决此问题吗?


.eslintrc.js文件中添加以下行

1
2
3
"env": {
   "jest": true
}

要么

1
2
3
4
5
6
{
 "plugins": ["jest"]
},
"env": {
 "jest/globals": true
}

有关更多详细信息,请在此处进行检查,并对其进行定义。

希望您安装了eslint-plugin-jest软件包。
文档。

配置ESLint的所有配置详细信息。


在文件开头添加以下注释行

1
/* globals describe, expect, it */