Created
May 30, 2023 21:37
-
-
Save squarism/28efb4a2ae2138f69d912c05c7468c79 to your computer and use it in GitHub Desktop.
Gitlab and Vitest Code Coverage with c8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# stages: | |
# - whatever | |
# - test | |
vitest: | |
stage: test | |
script: | |
# assuming scripts: has "test": "vitest" ... | |
# run your tests with coverage output | |
- npm test -- run --coverage | |
coverage: '/All files\s+\|\s+\d+\.\d+\s+\|\s+\d+\.\d+\s+\|\s+\d+\.\d+\s+\|\s+(\d+\.\d+)\s+.*/' | |
# Now your Gitlab MRs will have code coverage deltas attached to them |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Vitest comes with c8 code coverage (even if you have to install a package) but Gitlab doesn't understand it. | |
If you add a `scripts:` command for `coverage: npm run vitest run --coverage` then you can run `npm run coverage`. | |
It will prompt you to install c8 [Yn] basically, so I won't list the package you need to install. | |
You can configure vitest to report out `'default'` as a reporter and then your code coverage will be displayed as | |
STDOUT in the CI job. But Gitlab won't understand the output. There is no need to use file output or artifacts | |
because you will not be parsing a file. You can just configure Gitlab with a regex to get the output as shown. | |
c8 output looks like this | |
% Coverage report from c8 | |
------------------------------------------|---------|----------|---------|---------|--------------------- | |
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | |
------------------------------------------|---------|----------|---------|---------|--------------------- | |
All files | 1.23 | 1.23 | 1.23 | 1.23 |% | |
--------------------------------------------------------------------------------------------------------- | |
Annoying because we can't easily get a total. So, we will just use this top line and the '% Lines' as the coverage metric. | |
Maybe this is overly simplistic. It will be a monster regex and it's too bad we can't use the columns easily but here we are. |
thanks for this useful ready to use c8 regex coverage
sample 👍
then you could use junit report too if for example your mocha is run with this args --reporter mocha-junit-reporter --reporter-options mochaFile=junit.xml
,
and then add to your .gitlab-ci.yml
the report path
artifacts:
when: always
reports:
junit:
- junit.xml
I shortened the regex to /All files(?:[^|]*\|){4}\s*(\S+)/
, where you can change the 4 to use a different column
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice