【问题标题】:describe is not defined when installing jest安装 jest 时未定义 describe
【发布时间】:2019-09-12 10:31:50
【问题描述】:

我在我的项目中安装了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)

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

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

【问题讨论】:

    标签: jestjs eslint eslint-config-airbnb eslintrc


    【解决方案1】:

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

    "env": {
        "jest": true
    }
    

    {
      "plugins": ["jest"]
    },
    "env": {
      "jest/globals": true
    }
    

    更多细节请查看here,它的定义也是一样的。

    希望你安装了eslint-plugin-jest 包。如果不是请通过 Documentation.

    Configuring ESLint的所有配置详情。

    【讨论】:

      【解决方案2】:

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

      /* globals describe, expect, it */ 
      

      【讨论】:

        【解决方案3】:

        对于 jest 27,所有这些都不应该是必要的。
        杀死node_modules & package-lock.json.
        然后运行npm i

        【讨论】:

          猜你喜欢
          • 2019-04-03
          • 2021-07-02
          • 2015-02-21
          • 2021-02-19
          • 1970-01-01
          • 2017-08-31
          • 1970-01-01
          • 2019-12-13
          • 1970-01-01
          相关资源
          最近更新 更多