Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

javascript - 如何在Jest中使用ESLint(How to use ESLint with Jest)

I'm attempting to use the ESLint linter with the Jest testing framework.

(我正在尝试将ESLint linter与Jest测试框架一起使用。)

Jest tests run with some globals like jest , which I'll need to tell the linter about;

(玩笑测试与诸如jest类的一些全局变量一起运行,我需要告诉短绒猫有关;)

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:

(但是棘手的是目录结构,使用Jest,测试将源代码嵌入到__tests__文件夹中,因此目录结构类似于:)

src
    foo
        foo.js
        __tests__
            fooTest.js
    bar
        bar.js
        __tests__
            barTest.js

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文件以添加全局变量...,但是我当然不想在每个__test__目录中都添加.eslintrc文件。)

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.

(现在,我只是将测试全局变量添加到全局.eslintrc文件中,但是由于这意味着我现在可以在非测试代码中引用jest ,因此,这似乎不是“正确的”解决方案。)

Is there a way to get eslint to apply rules based on some pattern based on the directory name, or something like that?

(有没有办法让eslint根据基于目录名称的某种模式来应用规则,或者类似的方法?)

  ask by Retsam translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The docs show you are now able to add:

(文档显示您现在可以添加:)

"env": {
    "jest": true
}

To your .eslintrc which will add all the jest related things to your environment, eliminating the linter errors/warnings.

(到您的.eslintrc ,它将把所有与玩笑相关的内容添加到您的环境中,从而消除了linter错误/警告。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...