modernweb-dev / web

Guides, tools and libraries for modern web development.
https://modern-web.dev
MIT License
2.23k stars 297 forks source link

[test-runner] Support per-file code coverage threshold restrictions #2834

Open miyasudokoro opened 1 month ago

miyasudokoro commented 1 month ago

There is a configuration "per-file" that exists in code-coverage libraries such as c8 and nyc that allows restricting code coverage metric thresholds per each file rather than in the aggregate or summary of all files. It would be very nice to have this feature in the test-runner code coverage.

Here's an example for why someone would want this feature. Let's say you have the following coverage config and code coverage summary:

{
    "threshold": {
        "lines": 30,
        "functions": 30,
        "statements": 30,
        "branches": 30
    },
    "perFile": false,
    .... other configurations
}

--------------------------------------|---------|----------|---------|---------|-------------------------------------------
File                                  | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------------------------------|---------|----------|---------|---------|-------------------------------------------
All files                             |   61.75 |    83.92 |   31.77 |   61.75 |                                                                         
 src/js                               |   62.38 |    85.45 |   32.07 |   62.38 |                                           
  notcovered.js                       |       0 |        0 |       0 |       0 | 1-6                                       
  templater.js                        |   62.63 |    87.03 |   32.38 |   62.63 | ...270-1272,1277-1281,1412-1432,1459-1461 
--------------------------------------|---------|----------|---------|---------|-------------------------------------------

This code coverage passes because the "All files" summary percentages are all above 30%. However, if you set "perFile" to true, then this code coverage would fail because the notcovered.js file is only at 0%.

I am willing to implement this myself. I have always had "per-file" turned on in the past, so I would like to be able to use it here as well.