Skip to content

Instantly share code, notes, and snippets.

@squarism
Created May 30, 2023 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save squarism/28efb4a2ae2138f69d912c05c7468c79 to your computer and use it in GitHub Desktop.
Save squarism/28efb4a2ae2138f69d912c05c7468c79 to your computer and use it in GitHub Desktop.
Gitlab and Vitest Code Coverage with c8
# 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
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.
@gabrielyotoo
Copy link

nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment