Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to generate Jest / Istanbul coverage report in junit.xml format

I'd like to use Gitlab CI for tracking / embedding the coverage analysis in merge requests. The gitlab-ci.yml artifacts:reports:junit config option seems to be suitable for this task. However, this requires the coverage output to be in junit.xml format.

I don't find a proper setup for outputting the coverage this format. Neither can I find a tool to convert from lcov/json/clover to junit.xml .

like image 331
lipp Avatar asked Mar 05 '19 12:03

lipp


People also ask

How do I get jest coverage?

To get Jest up & running, install it as a dev dependency in your project. For ease of use, also in your CI/CD pipelines, I recommend to add script entries for running Jest with and without collecting coverage statistics in your package.


1 Answers

It can be done with the jest-junit reporter plugin. https://github.com/jest-community/jest-junit

yarn add --dev jest-junit

Then execute this locally to see if it is working

yarn test --colors --coverage --reporters=default --reporters=jest-junit

And you'll see a junit.xml file in root.

Configure your .gitlab-ci.yml like this to then see the output in Gitlab:

test:
  stage: test
  coverage: /All files[^|]*\|[^|]*\s+([\d\.]+)/
  artifacts:
    reports:
      junit: junit.xml
  script:
    - yarn test --colors --coverage --reporters=default --reporters=jest-junit

Oh and add the coverage/ folder and junit.xml to .gitignore so they aren't added to the git repo.

This all seems to work ok in a Create React App project too

like image 140
Jeff Sheets Avatar answered Sep 22 '22 23:09

Jeff Sheets