Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage from Jest to stdout to GitLab

Tags:

I am running jest test with code coverage in GitLab CI and GitLab captures the percentage from stdout of a runner in gitlab.

jest --coverage produces the coverage in stdout and gitlab captures it using /All files[^|]*\|[^|]*\s+([\d\.]+)/ regexp but when I run jest --coverage --json --outputFile=xyz.json sadly jest doesn't print the code coverage to stdout.

What can I do to get code coverage in stdout from jest when --json arguments is given to jest?

jest version : v22.4.3 same for jest-cli

like image 699
Dhaval Lila Avatar asked Jun 11 '18 13:06

Dhaval Lila


People also ask

What is jest statement coverage?

if you have a line of code that says var x= 10; console. log(x); that's one line and 2 statements. Statement coverage has each statement in the program been executed. Line coverage has each executable line in the source file been executed.


2 Answers

The following configuration will let GitLab interpret the coverage report generated by Jest:

stages:   - test  Unit tests:   image: node:12.17.0   stage: test   script:     - jest --coverage   coverage: /All\sfiles.*?\s+(\d+.\d+)/ 

There's an open issue on GitLab which contains the correct regex for coverage reports generated using Jest (which is used by Create React App).

like image 142
ndequeker Avatar answered Sep 25 '22 08:09

ndequeker


I'm using the following regex to parse the text-summary coverage reports from Jest for Gitlab: ^(?:Statements|Branches|Functions|Lines)\s*:\s*([^%]+)

Note that Gitlab will only consider the last match though. So above could be written as ^Lines\s*:\s*([^%]+). I included the full example so that you can choose the one that makes the most sense for your project.

The "text-summary" report looks like this in StdOut:

=============================== Coverage summary =============================== Statements   : 80.49% ( 2611/3244 ) Branches     : 65.37% ( 923/1412 ) Functions    : 76.48% ( 582/761 ) Lines        : 80.44% ( 2583/3211 ) ================================================================================ 

Make sure you have included text-summary as a coverage reporter in your jest.config.js:

coverageReporters: ['text-summary', 'lcov', 'cobertura'], 
like image 33
mediafreakch Avatar answered Sep 23 '22 08:09

mediafreakch