我正在使用 Jest 编写一些规范,并使用 ESLint 对样式进行 lint。
对于我的 foo.spec.js
测试,eslint 不断抛出以下错误。似乎认为 jest
、beforeEach
、afterEach
等...未在该文件中定义。
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/