linst Jest测试文件时,eslint抛出“ no-undef”错误

user2490003:

我正在使用Jest编写一些规范,并使用ESLint编写样式。

对于我的foo.spec.js测试,eslint不断抛出以下错误。它似乎认为jestbeforeEachafterEach,等...没有在该文件中定义。

   11:1   error  'beforeEach' is not defined  no-undef
   12:3   error  'jest' is not defined        no-undef
   14:13  error  'jest' is not defined        no-undef
   18:1   error  'afterEach' is not defined   no-undef
   20:1   error  'describe' is not defined    no-undef
   21:3   error  'it' is not defined          no-undef
   25:5   error  'expect' is not defined      no-undef
   28:5   error  'expect' is not defined      no-undef
   31:5   error  'expect' is not defined      no-undef
   34:3   error  'it' is not defined          no-undef
   38:5   error  'expect' is not defined      no-undef
   41:5   error  'jest' is not defined        no-undef
   42:5   error  'expect' is not defined      no-undef
   43:5   error  'expect' is not defined      no-undef
   46:3   error  'it' is not defined          no-undef
   54:5   error  'expect' is not defined      no-undef
   58:5   error  'jest' is not defined        no-undef

我相信这些被jest自动包含,因此不需要在我的spec文件中显式导入它们。实际上,我通过jest.setup.js文件导入的唯一内容

import "react-testing-library/cleanup-after-each";
import "jest-dom/extend-expect";

有没有一种方法可以消除这些错误而不必禁用每个文件或内联文件顶部的eslint规则?

谢谢!

詹德森·康斯坦丁诺:

请将以下内容添加到您的.eslintrc文件中:

{
  "overrides": [
    {
      "files": [
        "**/*.spec.js",
        "**/*.spec.jsx"
      ],
      "env": {
        "jest": true
      }
    }
  ]
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

解决文档的linter错误no-undef

当未在命令行中指定配置文件时,Spring Boot集成测试将无法获取默认配置文件并抛出错误

运行Jest测试时出现意外的令牌“导入”错误?

错误“条带”未定义no-undef

错误'NODE_ENV'未定义no-undef eslint

ESLint报告基于流类型的全局类型的no-undef错误

ESLint no-unused-vars和no-undef错误,但使用了变量

如何解决setTimeout的no-undef错误

用Jest测试ES6类会抛出“不是构造函数”错误

错误'_'未定义no-undef

Erlang undef错误

使用toHaveBeenNthCalledWith时反应Jest测试错误

我如何静默地对抛出的错误进行Jest测试

文件/ blob-eslint(no-undef)

在运行测试用例ClientHttpResponse时抛出401错误

为什么在运行react js组件类时出现“ no-undef”错误?

测试Sage Evolution节点连接时抛出Droppoint错误

Eslint和VueJS文件。抛出错误但无法解决

测试node.crypto函数时,mocha抛出错误

方案:错误:#<undef> 不是函数

错误:“$”未定义。[no-undef]

eslint 抛出错误

运行 jest 测试命令时,React 本机打字稿抛出错误

如何忽略 Jest 中的空测试文件抛出错误

尝试渲染反应组件时,未定义“名称框”反应/jsx-no-undef 错误

当我尝试在 libburn 中编译测试文件夹时,gcc 抛出错误

即使变量在 IF 语句中,ESLint 也会抛出 no-undef

Eslint 修复即使在 json 文件上也抛出错误?

JavaScript。错误:未定义 no-undef