Jest カバレッジレポート [coverageReporters]

# はじめに

Jestの[カバレッジレポート](https://github.com/facebook/jest/blob/main/docs/Configuration.md#coveragereporters-arraystring--string-options)について調べる機会がありました。
レポートの作成には、[istanbul-reports](
https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-reports/lib)が利用されているようで、そのオプションまとめになります。

環境は、Jest 29.3.1 を利用しています。

## default
```
coverageReporters: ["clover", "json", "lcov", "text"]
```

# coverageReporters

## text
```
coverageReporters: ["text"]
```
```text:console output
------------|---------|----------|---------|---------|-------------------------------------------------------------------------
File        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s                                                       
------------|---------|----------|---------|---------|-------------------------------------------------------------------------
All files   |      82 |    83.87 |   59.25 |      82 |                                                                         
 base.js    |       0 |        0 |       0 |       0 | 1-43                                                                    
 comment.js |    94.8 |       80 |      75 |    94.8 | 112-113,116-117,142-145                                                 
 info.js    |   85.75 |    89.47 |   52.94 |   85.75 | 132-139,156-164,167-168,210-216,222-223,229-230,238-239,258-266,294-298 
 load.js    |     100 |      100 |     100 |     100 |                                                                         
------------|---------|----------|---------|---------|-------------------------------------------------------------------------

```
```text:coverage/
[なし]
```

## text-summary
```
coverageReporters: ["text-summary"]
```
```text:console output
=============================== Coverage summary ===============================
Statements   : 82% ( 442/539 )
Branches     : 83.87% ( 26/31 )
Functions    : 59.25% ( 16/27 )
Lines        : 82% ( 442/539 )
================================================================================
```
```text:coverage/
[なし]
```

## json
```
coverageReporters: ["json"]
```
```text:console output
[なし]
```
```text:coverage/
coverage-final.json
```
```text:coverage-final.json
{"/js/base.js": {"path":"/js/base.js","all":true,"statementMap":{"0":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}},"1":{"start":{"line":2,"column":0},"end":{"line":2,"column":0}},"2":{"start":{"line":3,"column":0},"end":{"line":3,"column":23}},"3":{"start":{"line":4,"column":0},"end":{"line":4,"column":41}},"4":{"start":{"line":5,"column":0},"end":{"line":5,"column":13}},"5":{"start":{"line":6,"column":0},"end":{"line":6,"column":114}},"6":{"start":{"line":7,"column":0},"end":{"line":7,"column":124}},"7":{"start":{"line":8,"column":0},"end":{"line":8,"column":16}},"8":{"start":{"line":9,"column":0},"end":{"line":9,"column":23}},"9":{"start":{"line":10,"column":0},"end":{"line":10,"column":14}},"10":{"start":{"line":11,"column":0},"end":{"line":11,"column":6}},"11":{"start":{"line":12,"column":0},"end":{"line":12,"column":12}},"12":{"start":{"line":13,"column":0},"end":{"line":13,"column":14}},"13":{"start":{"line":14,"column":0},"end":{"line":14,"column":18}},"14":{"start":{"line":15,"column":0},"end":{"line":15,"column":40}},"15":{"start":{"line":16,"column":0},"end":{"line":16,"column":6}},"16":{"start":{"line":17,"column":0},"end":{"line":17,"column":4}},"17":{"start":{"line":18,"column":0},"end":{"line":18,"column":10}},"18":{"start":{"line":19,"column":0},"end":{"line":19,"column":24}},"19":{"start":{"line":20,"column":0},"end":{"line":20,"column":31}},"20":{"start":{"line":21,"column":0},"end":{"line":21,"column":33}},"21":{"start":{"line":22,"column":0},"end":{"line":22,"column":49}},"22":{"start":{"line":23,"column":0},"end":{"line":23,"column":48}},"23":{"start":{"line":24,"column":0},"end":{"line":24,"column":47}},"24":{"start":{"line":25,"column":0},"end":{"line":25,"column":14}},"25":{"start":{"line":26,"column":0},"end":{"line":26,"column":33}},"26":{"start":{"line":27,"column":0},"end":{"line":27,"column":52}},"27":{"start":{"line":28,"column":0},"end":{"line":28,"column":49}},"28":{"start":{"line":29,"column":0},"end":{"line":29,"column":49}},"29":{"start":{"line":30,"column":0},"end":{"line":30,"column":7}},"30":{"start":{"line":31,"column":0},"end":{"line":31,"column":6}},"31":{"start":{"line":32,"column":0},"end":{"line":32,"column":4}},"32":{"start":{"line":33,"column":0},"end":{"line":33,"column":13}},"33":{"start":{"line":34,"column":0},"end":{"line":34,"column":23}},"34":{"start":{"line":35,"column":0},"end":{"line":35,"column":31}},"35":{"start":{"line":36,"column":0},"end":{"line":36,"column":45}},"36":{"start":{"line":37,"column":0},"end":{"line":37,"column":14}},"37":{"start":{"line":38,"column":0},"end":{"line":38,"column":18}},"38":{"start":{"line":39,"column":0},"end":{"line":39,"column":7}},"39":{"start":{"line":40,"column":0},"end":{"line":40,"column":6}},"40":{"start":{"line":41,"column":0},"end":{"line":41,"column":4}},"41":{"start":{"line":42,"column":0},"end":{"line":42,"column":2}},"42":{"start":{"line":43,"column":0},"end":{"line":43,"column":29}}},"s":{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0,"14":0,"15":0,"16":0,"17":0,"18":0,"19":0,"20":0,"21":0,"22":0,"23":0,"24":0,"25":0,"26":0,"27":0,"28":0,"29":0,"30":0,"31":0,"32":0,"33":0,"34":0,"35":0,"36":0,"37":0,"38":0,"39":0,"40":0,"41":0,"42":0},"branchMap":{"0":{"type":"branch","line":1,"loc":{"start":{"line":1,"column":0},"end":{"line":43,"column":29}},"locations":[{"start":{"line":1,"column":0},"end":{"line":43,"column":29}}]}},"b":{"0":[0]},"fnMap":{"0":{"name":"(empty-report)","decl":{"start":{"line":1,"column":0},"end":{"line":43,"column":29}},"loc":{"start":{"line":1,"column":0},"end":{"line":43,"column":29}},"line":1}},"f":{"0":0}}
,"/js/comment.js": {...[省略]...
,"/js/info.js": {...[省略]...
,"/js/load.js": {...[省略]...
}

```

## json-summary
```
coverageReporters: ["json-summary"]
```
```text:console output
[なし]
```
```text:coverage/
coverage-summary.json
```
```text:coverage-summary.json
{"total": {"lines":{"total":539,"covered":442,"skipped":0,"pct":82},"statements":{"total":539,"covered":442,"skipped":0,"pct":82},"functions":{"total":27,"covered":16,"skipped":0,"pct":59.25},"branches":{"total":31,"covered":26,"skipped":0,"pct":83.87},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":"Unknown"}}
,"/js/base.js": {"lines":{"total":43,"covered":0,"skipped":0,"pct":0},"functions":{"total":1,"covered":0,"skipped":0,"pct":0},"statements":{"total":43,"covered":0,"skipped":0,"pct":0},"branches":{"total":1,"covered":0,"skipped":0,"pct":0}}
,"/js/comment.js": {...[省略]...
,"/js/info.js": {...[省略]...
,"/js/load.js": {...[省略]...
}
```


## lcov
```
coverageReporters: ["lcov"]
```
```text:console output
[なし]
```
```text:coverage/
lcov-report/base.css
lcov-report/base.js.html
lcov-report/block-navigation.js
lcov-report/comment.js.html
lcov-report/favicon.png
lcov-report/index.html
lcov-report/info.js.html
lcov-report/load.js.html
lcov-report/prettify.css
lcov-report/prettify.js
lcov-report/sort-arrow-sprite.png
lcov-report/sorter.js
lcov.info
```
**lcov-report/index.html**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/251940/c2a54e86-e6ad-fb8d-5464-e18ed164cb89.png)

## lcovonly
```
coverageReporters: ["lcovonly"]
```
```text:console output
[なし]
```
```text:coverage/
lcov.info
```

## text-lcov
```
coverageReporters: ["text-lcov"]
```
```text:console output
TN:
SF:js/base.js
FN:1,(empty-report)
FNF:1
FNH:0
...[省略]...
LH:19
BRDA:19,0,0,3
BRF:1
BRH:1
end_of_record
```
```text:coverage/
[なし]
```

## clover
```
coverageReporters: ["clover"]
```
```text:console output
[なし]
```
```text:coverage/
clover.xml
```
```xml:clover.xml
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1672190096411" clover="3.2.0">
  <project timestamp="1672190096412" name="All files">
    <metrics statements="539" coveredstatements="442" conditionals="31" coveredconditionals="26" methods="27" coveredmethods="16" elements="597" coveredelements="484" complexity="0" loc="539" ncloc="539" packages="1" files="4" classes="4"/>
    <file name="base.js" path="/js/base.js">
      <metrics statements="43" coveredstatements="0" conditionals="1" coveredconditionals="0" methods="1" coveredmethods="0"/>
      <line num="1" count="0" type="cond" truecount="0" falsecount="1"/>
...[省略]...
      <line num="16" count="1" type="stmt"/>
      <line num="17" count="1" type="stmt"/>
      <line num="18" count="1" type="stmt"/>
      <line num="19" count="1" type="cond" truecount="1" falsecount="0"/>
    </file>
  </project>
</coverage>
```

## cobertura
```
coverageReporters: ["cobertura"]
```
```text:console output
[なし]
```
```text:coverage/
cobertura-coverage.xml
```
```xml:cobertura-coverage.xml
<?xml version="1.0" ?>
<!DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-04.dtd">
<coverage lines-valid="539" lines-covered="442" line-rate="0.82" branches-valid="31" branches-covered="26" branch-rate="0.8387" timestamp="1672190365374" complexity="0" version="0.1">
  <sources>
    <source>/proj</source>
  </sources>
  <packages>
    <package name="main" line-rate="0.82" branch-rate="0.8387">
      <classes>
...[省略]...
      </classes>
    </package>
  </packages>
</coverage>
```

## html-spa
```
coverageReporters: ["html-spa"]
```
```text:console output
[なし]
```
```text:coverage/
base.css
base.js.html
block-navigation.js
bundle.js
comment.js.html
favicon.png
index.html
info.js.html
load.js.html
prettify.css
prettify.js
sort-arrow-sprite.png
sorter.js
spa.css
```
**index.html**
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/251940/ed355618-007e-8916-9034-7df8664284ba.png)



## teamcity
```
coverageReporters: ["teamcity"]
```
```text:console output
##teamcity[blockOpened name='Code Coverage Summary']
##teamcity[buildStatisticValue key='CodeCoverageAbsBCovered' value='442']
##teamcity[buildStatisticValue key='CodeCoverageAbsBTotal' value='539']
##teamcity[buildStatisticValue key='CodeCoverageAbsRCovered' value='26']
##teamcity[buildStatisticValue key='CodeCoverageAbsRTotal' value='31']
##teamcity[buildStatisticValue key='CodeCoverageAbsMCovered' value='16']
##teamcity[buildStatisticValue key='CodeCoverageAbsMTotal' value='27']
##teamcity[buildStatisticValue key='CodeCoverageAbsLCovered' value='442']
##teamcity[buildStatisticValue key='CodeCoverageAbsLTotal' value='539']
##teamcity[blockClosed name='Code Coverage Summary']
```
```text:coverage/
[なし]
```

## none
```
coverageReporters: ["none"]
```
```text:console output
[なし]
```
```text:coverage/
[なし]
```


# 参考
https://github.com/facebook/jest/blob/main/docs/Configuration.md#coveragereporters-arraystring--string-options

https://github.com/istanbuljs/istanbuljs/tree/master/packages/istanbul-reports/lib