简体   繁体   English

eslint 在检查 Jest 测试文件时抛出 `no-undef` 错误

[英]eslint throws `no-undef` errors when linting Jest test files

I'm using Jest to write some specs and ESLint to lint the styling.我正在使用 Jest 来编写一些规范,并使用 ESLint 来检查样式。

For my foo.spec.js tests, eslint keeps throwing the following errors.对于我的foo.spec.js测试,eslint 不断抛出以下错误。 It seems to think that jest , beforeEach , afterEach , etc... are not defined in that file.似乎认为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

I believe those are included by jest automatically and so they don't need to be explicitly imported in my spec files.我相信这些是由 jest 自动包含的,因此不需要在我的规范文件中明确导入。 In fact the only thing I import via my jest.setup.js file is事实上,我通过我的jest.setup.js文件导入的唯一东西是

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

Is there a way to eliminate these errors without having to disable eslint rules at the top of each individual file or inline?有没有一种方法可以消除这些错误,而不必在每个单独文件或内联文件的顶部禁用 eslint 规则?

Thanks!谢谢!

Please, add the following to your .eslintrc file:请将以下内容添加到您的 .eslintrc 文件中:

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

You do not need to add this override option and specify in which files jest should be available.您无需添加此覆盖选项并指定jest应该在哪些文件中可用。 Adding only env field should be enough.仅添加env字段就足够了。

.eslintrc.js : .eslintrc.js

module.exports = {
  env: {
    jest: true
  },
//...
}

Had a similar problem with eslint throwing no-undef errors for my jest setup.js file with some jest.mocks in it.有一个类似的问题,eslint 为我的 jest setup.js 文件抛出 no-undef 错误,其中包含一些 jest.mocks。

Ended up fixing it by using the ESLint plugin for Jest .最终通过使用ESLint 插件来修复它 Jest

After installing the plugin as a dev dependency, I merged the following into my .eslintrc config file per instructions from the plugin readme.将插件安装为开发依赖项后,我根据插件自述文件中的说明将以下内容合并到我的 .eslintrc 配置文件中。

{
  "extends": ["plugin:jest/recommended"],
  "plugins": ["jest"]
}

Then the no-undef errors are gone.然后 no-undef 错误消失了。

As I am using a StandardJS Linter none of the suggested solution worked for me.因为我使用的是 StandardJS Linter,所以建议的解决方案都不适合我。 Instead, I had to define a global for "it" within my standard-config.相反,我必须在我的标准配置中为“它”定义一个全局变量。

Eg in package.json :例如在package.json中:

...
  "standard": {
    "parser": "babel-eslint",
    "globals": [
      "afterAll",
      "beforeAll",
      "afterEach",
      "jasmine",
      "describe",
      "test",
      "jest",
      "expect",
      "it"
    ]
 }
...

The environment can also be applied to individual files (without modifying configuration files).该环境也可以应用于单个文件(无需修改配置文件)。

For example, use this comment at the top of the file:例如,在文件顶部使用此注释:

/* eslint-env jest */

This can be useful when there are some files (eg jest.setup.js ), that access jest.当有一些文件(例如jest.setup.js )访问jest. and are outside the regular test folders.并且在常规测试文件夹之外

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM