linst Jest测试文件时,eslint抛出“ no-undef”错误 [英] eslint throws `no-undef` errors when linting Jest test files

查看:524
本文介绍了linst Jest测试文件时,eslint抛出“ no-undef”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Jest to write some specs and ESLint to lint the styling.

对于我的 foo.spec.js 测试,eslint不断抛出以下错误。似乎认为笑话 beforeEach afterEach ,等等...在该文件中未定义。

For my foo.spec.js tests, eslint keeps throwing the following errors. It seems to think that jest, beforeEach, afterEach, etc... are not defined in that file.

   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 文件导入的唯一内容是

I believe those are included by jest automatically and so they don't need to be explicitly imported in my spec files. In fact the only thing I import via my jest.setup.js file is

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

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

Is there a way to eliminate these errors without having to disable eslint rules at the top of each individual file or inline?

谢谢!

推荐答案

请在您的中添加以下内容。 eslintrc文件:

Please, add the following to your .eslintrc file:

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

这篇关于linst Jest测试文件时,eslint抛出“ no-undef”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆