26

使用 mocha 和 instanbul 时,如何从覆盖率报告中排除文件夹和文件(按路径)?

我想通过配置排除而不是

/*istanbul ignore next*/

在每个文件中。

(Jenkins使用的生成报告)

谢谢,

4

4 回答 4

21

-x您可以使用该参数忽略与特定模式匹配的文件。

 istanbul help cover

 ...
 -x <exclude-pattern> [-x <exclude-pattern>]
        one or more fileset patterns e.g. "**/vendor/**"
 ...
于 2015-06-07T14:09:45.173 回答
16

如果你运行istanbul help config,你会看到 istanbul 的默认配置。您可以将默认配置复制/粘贴到.istanbul.yml源代码树根目录的文件中,然后将排除项存储在其中。

这是我的样子(这使得排除许多目录变得容易):

verbose: false
instrumentation:
    root: .
    extensions:
        - .js
    default-excludes: true
    excludes: ['**/tinymce/**', '**/lib/**', '**/tools/**', '**/build/**']
    embed-source: false
    variable: __coverage__
    compact: true
    preserve-comments: false
    complete-copy: false
    save-baseline: false
    baseline-file: ./coverage/coverage-baseline.json
    include-all-sources: true
    include-pid: false
    es-modules: false
reporting:
    print: summary
    reports:
        - lcov
    dir: ./tools/coverage
    watermarks:
        statements: [50, 80]
        lines: [50, 80]
        functions: [50, 80]
        branches: [50, 80]
    report-config:
        clover: {file: clover.xml}
        cobertura: {file: cobertura-coverage.xml}
        json: {file: coverage-final.json}
        json-summary: {file: coverage-summary.json}
        lcovonly: {file: lcov.info}
        teamcity: {file: null, blockName: Code Coverage Summary}
        text: {file: null, maxCols: 0}
        text-lcov: {file: lcov.info}
        text-summary: {file: null}
hooks:
    hook-run-in-context: false
    post-require-hook: null
    handle-sigint: false
check:
    global:
        statements: 0
        lines: 0
        branches: 0
        functions: 0
        excludes: []
    each:
        statements: 0
        lines: 0
        branches: 0
        functions: 0
        excludes: []
于 2016-01-08T20:03:16.833 回答
5

在您的情况下,我将使用以下内容:

istanbul -x "**/pattern/to/exclude/**" cover _mocha -- --recursive -R tap test/ > test.tap && istanbul report clover –  snoof 9 hours ago 

您可以通过添加多个-x选项来排除多个模式。

于 2015-06-07T22:01:24.870 回答
4

感谢您的建议,

这是解决方案:

istanbul cover -x '**/config/**'  _mocha -- --recursive -R tap test/ > test.tap && istanbul report clover
于 2015-06-08T14:13:11.933 回答