Today I spent the whole day trying to get my test coverage to show in SonarCloud. I want to write down my findings so you don’t have to waste as much time as I did.

Repo Structure

It’s a pretty simple setup. I’m using Jest (version 25.1.0) installed globally with npm install jest -g (cos why not) with tests located in the test directory.

Installing Jest Sonar Reporter

You cannot pass Jest coverage data directly into SonarCloud, so you need to use an intermediary to convert it into a format SonarCloud can understand.

SonarCloud recommends using jest-sonar-reporter to do this. You need to npm install jest-sonar-reporter --save-dev and then you need to update the jest settings in thepackage.json file to include "testResultsProcessor": "jest-sonar-reporter" (I already added it to the sample above).

When you’re done editing, commit this file to your repo (you’ll find it in .github/workflows/main.yml).

It’s not the most complex workflow. Here’s what we do:

  1. Fetch the master branch (we don’t merge it though — this fixes an error in SonarCloud).
  2. Install project dependencies.
  3. Install Jest.
  4. Run Tests on PR.
  5. Run SonarCloud scan.

You’ll need to setup secrets in your repo for the SONAR_TOKEN (You create tokens on sonarcloud.io. You don’t need to create a GITHUB_TOKEN as Github creates it automatically.

And that’s it. Whenever you make a new PR or merge to master, it triggers the workflow, running the tests and sending it to SonarCloud.

#coding #javascript #testing #programming

How to Make SonarCloud Play-Along Nicely with Jest
5.35 GEEK