如何在Jest中使用ESLint [英] How to use ESLint with Jest
问题描述
我正在尝试将ESLint linter与Jest测试框架一起使用。
I'm attempting to use the ESLint linter with the Jest testing framework.
Jest测试使用一些全局变量运行,如 jest
,我需要告诉linter;但棘手的是目录结构,Jest测试嵌入了 __ tests __
文件夹中的源代码,因此目录结构如下所示:
Jest tests run with some globals like jest
, which I'll need to tell the linter about; but the tricky thing is the directory structure, with Jest the tests are embedded with the source code in __tests__
folders, so the directory structure looks something like:
src
foo
foo.js
__tests__
fooTest.js
bar
bar.js
__tests__
barTest.js
通常,我' d在单个目录下进行所有测试,我可以在那里添加一个 .eslintrc
文件来添加全局...但我当然不想添加 .eslintrc
文件到每一个 __ test __
dir。
Normally, I'd have all my tests under a single dir, and I could just add an .eslintrc
file there to add the globals... but I certainly don't want to add a .eslintrc
file to every single __test__
dir.
目前,我刚刚将测试全局变量添加到全局 .eslintrc
文件中,但由于这意味着我现在可以在非测试代码中引用 jest
,这似乎不是正确的解决方案。
For now, I've just added the test globals to the global .eslintrc
file, but since that means I could now reference jest
in non-testing code, that doesn't seem like the "right" solution.
有没有办法让eslint根据某些基于目录名称的模式应用规则,或类似的东西?
Is there a way to get eslint to apply rules based on some pattern based on the directory name, or something like that?
推荐答案
文档显示您现在可以添加:
The docs show you are now able to add:
"env": {
"jest": true
}
到 .eslintrc
这将添加所有与你的环境相关的东西,消除了linter错误/警告。
To your .eslintrc
which will add all the jest related things to your environment, eliminating the linter errors/warnings.
希望有所帮助!
这篇关于如何在Jest中使用ESLint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!