我正在尝试将ESLint linter与Jest测试框架一起使用.
Jest测试用一些全局变量运行jest,我需要告诉linter; 但是棘手的是目录结构,使用Jest测试嵌入了__tests__文件夹中的源代码,因此目录结构类似于:
src
foo
foo.js
__tests__
fooTest.js
bar
bar.js
__tests__
barTest.js
Run Code Online (Sandbox Code Playgroud)
通常情况下,我在单个目录下进行所有测试,我可以在.eslintrc那里添加一个文件来添加全局...但我当然不想.eslintrc在每个__test__目录中添加一个文件.
现在,我刚刚将测试全局变量添加到全局.eslintrc文件中,但由于这意味着我现在可以jest在非测试代码中引用,这似乎不是"正确"的解决方案.
有没有办法让eslint基于某些基于目录名称的模式应用规则,或类似的东西?
我刚刚生成了一个新项目,npx react-native initESLint 在我的一个测试文件中抱怨:
9:1 error 'describe' is not defined no-undef
12:5 error 'beforeEach' is not defined no-undef
16:5 error 'afterEach' is not defined no-undef
20:5 error 'test' is not defined no-undef
28:17 error 'fail' is not defined no-undef
30:13 error 'expect' is not defined no-undef
Run Code Online (Sandbox Code Playgroud)
env: {
jest: true,
},
Run Code Online (Sandbox Code Playgroud)
到我的.eslintrc.js文件。然而,ESLint 仍然抱怨:
28:17 error 'fail' is not defined no-undef
Run Code Online (Sandbox Code Playgroud)
有没有人已经经历过并解决了这个问题?
以下是 jest 中的依赖项版本package.json:
"babel-jest": "^26.5.2",
"jest": "^26.5.3",
Run Code Online (Sandbox Code Playgroud)