gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:36:13 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com