javascript 使用 Karma 和伊斯坦布尔时从覆盖范围中排除文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22518530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:17:46  来源:igfitidea点击:

Excluding files from coverage when using Karma and Istanbul

javascriptnode.jskarma-runneristanbulminmatch

提问by Joe

I am using Karma to test my JavaScript and get coverage reports. I am using the Istanbul coverage report, which is the default. Here is my preprocessors parameter:

我正在使用 Karma 来测试我的 JavaScript 并获取覆盖率报告。我正在使用伊斯坦布尔覆盖率报告,这是默认的。这是我的预处理器参数:

    preprocessors: {
        'framework/**/*.js':'coverage',            
        'framework/*.js':'coverage',
        '!framework/node/**/*.js':'coverage',         
        '!framework/test/**/*.js':'coverage',                                 
        'framework-lib/**/*.js':'coverage',
        '!framework-lib/tool-data-api/tool-data-api.js':'coverage'
    }

As you can see, I am trying to use the "!" as a negate command, which usually works with Node. However, it is not working here and none of my directories are being excluded.

如您所见,我正在尝试使用“!” 作为否定命令,通常与 Node.js 一起使用。但是,它在这里不起作用,我的目录都没有被排除在外。

Is there any way to do what I am trying to accomplish?

有什么办法可以做我想要完成的事情吗?

回答by Jon Hieb

According to https://karma-runner.github.io/0.12/config/configuration-file.html:

根据https://karma-runner.github.io/0.12/config/configuration-file.html

**/*.js: All files with a "js" extension in all subdirectories
**/!(jquery).js: Same as previous, but excludes "jquery.js"
**/(foo|bar).js: In all subdirectories, all "foo.js" or "bar.js" files

**/*.js:所有子目录中带有“js”扩展名的所有文件
**/!(jquery).js:与之前相同,但不包括“jquery.js”
**/(foo|bar).js:在所有子目录中,所有“foo.js”或“bar.js”文件

So, based on this, I tried the following:

因此,基于此,我尝试了以下操作:

preprocessors: {
    'framework/**/!(node|test)/*.js': 'coverage',
    'framework-lib/**/!(tool-data-api).js': 'coverage'
}

Which seems to have accomplished what you are looking for.

这似乎已经完成了你正在寻找的东西。

As a note to others who come here looking for how to target all files EXCEPT .spec.js files, try:

作为对来到这里寻找如何定位除 .spec.js 文件之外的所有文件的其他人的说明,请尝试:

'**/!(*spec).js'

Which seems to work for me.

这似乎对我有用。

回答by Alexandr Skachkov

In the Karma used minimatch.js lib for mathing files. So you need rewrite you rules. For instance to exclude folder node node it should has

在 Karma 中使用 minimatch.js 库作为数学文件。所以你需要重写你的规则。例如排除文件夹节点节点它应该有

preprocessors: {
        'framework/*[!node]/*.js':'coverage',            
    }

回答by joeycozza

I'm not sure if you are running "istanbul cover ..." to run your coverage report, but if you are, you can use the -x flag to exclude files/patterns. Typing in "istanbul help cover" will show you usage including this.

我不确定您是否正在运行“istanbul cover ...”来运行您的覆盖率报告,但如果是,您可以使用 -x 标志来排除文件/模式。输入“istanbul help cover”将显示包括此在内的用法。

 -x <exclude-pattern> [-x <exclude-pattern>]
              one or more fileset patterns e.g. "**/vendor/**"

回答by Ghanshayam Patel

Make sure that the directories / file you want to exclude are not loaded by any other include. for example, 'framework//.js':'coverage' will load files you are trying to exclude in '!framework/node//.js':'coverage'

确保您要排除的目录/文件没有被任何其他包含加载。例如,'framework/ / .js':'coverage' 将加载您试图在 '!framework/node//.js':'coverage' 中排除的文件