26

How can I exclude folders and files (by path) from coverage report when using mocha and instanbul?

I want to exclude by a configuration and not

/*istanbul ignore next*/

in each file.

(The generated report use by Jenkins)

Thanks,

Shai M.
  • 1,164
  • 3
  • 13
  • 28

4 Answers4

21

You can ignore files matching a certain pattern using the -x parameter.

 istanbul help cover

 ...
 -x <exclude-pattern> [-x <exclude-pattern>]
        one or more fileset patterns e.g. "**/vendor/**"
 ...
Blaise
  • 11,664
  • 8
  • 59
  • 89
  • How to do this in the Config Files, e.g. GruntFile? – Steve K Sep 08 '15 at 15:37
  • Depends on what grunt library you use to wrap istanbul. In `grunt-karma` I'm excluding a folder (`app/tests`) like this: `{..., preprocessors['app/{*.js,!(tests)/**/*.js}'] = ['coverage'], ...}` – Blaise Oct 19 '15 at 12:56
16

If you run istanbul help config you'll see istanbul's default configuration. You can copy/paste the default config into a .istanbul.yml file at the root of your source tree, then store the exclusions in it.

Here's what mine looks like (this makes it easy to exclude many directories):

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: []
Rob Johansen
  • 4,576
  • 7
  • 37
  • 68
  • Are you sure excludes works with include-all-sources set to true? instrumentation: root: src include-all-sources: true verbose: true excludes: ["\*\*/src/client/\*\*"] reporting: dir: "coverage" And I'm still getting client files in my report. – Will Lovett Feb 15 '17 at 22:20
4

I your case I would use the following:

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

You can exclude multiple patterns just by adding multiple -x options.

JME
  • 3,452
  • 12
  • 22
4

Thanks for the suggestions,

This was the solution:

istanbul cover -x '**/config/**'  _mocha -- --recursive -R tap test/ > test.tap && istanbul report clover
GhostCat
  • 127,190
  • 21
  • 146
  • 218
Shai M.
  • 1,164
  • 3
  • 13
  • 28