javascript - 对 Jest 测试文件进行 linting 测试时,eslint 抛出 `no-undef` 错误

标签 javascript unit-testing jestjs eslint

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

对于我的 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 自动包含的,因此不需要在我的规范文件中明确导入。事实上,我通过我的 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
      }
    }
  ]
}

关于javascript - 对 Jest 测试文件进行 linting 测试时,eslint 抛出 `no-undef` 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56398742/

相关文章:

vue.js - Vue2 : When testing component that contains a functional child component, 如何从 child 发出事件?

image - Expo Jest 找不到类似 'file@2x.png' 的图像

javascript - 在 Sencha Touch 2 中的 itemswipe 上显示复选框?

c# - 我是否正确编写了单元测试? NUnit + NSubstitute

javascript - 更新 DIV 并在 1 秒后使用 setTimeout 再次清空它

javascript - '错误 : Unexpected request' during Karma Angular Unit Test

.net - 有没有一种方法可以在VS2013中设置默认解决方案.runsettings文件?

node.js - 如何使用 Jest 测试使用 chalk 的 console.log?

javascript - 尝试使用 Merriam Webster 词典 API 返回的 XML,但请求失败。返回的状态为零。该怎么办?

javascript - 使用 JSONP 在 javascript 小部件中导航