SonarCloud - Nx monorepo - advice needed on how to setup coverage measures on main branch

  • ALM used GitHub
  • CI system used GithubActions
  • Scanner command used when applicable: Github action SonarSource/sonarcloud-github-action@master
  • Languages of the repository: TS

We work with the following setup:

  • we have an Nx monorepo
  • on our CI (that runs on both PRs and main branch) we only run the test affected by the changes made. That result of course in partial coverage figures gathered when running our tests. While this does not look to be an issue when sonarCloud analyses coverage on PRs new code, this approach does not look to work when on the main branch (coverage measured on latest change merged is used as final coverage of the main branch - resulting in jumping figures that do not reflect reality).

Does someone has advices on how to have an efficient setup in our case that allows to have correct coverage figures on main?

  • I’ve seen that there is a monorepo support option - but having 1 sonarCloud project for each monorepo project fragments a bit too much the coverage global figures and I’m not sure how it handles nx apps and libs shared by multiple apps
  • Running all tests in order to have complete coverage figures on master does not look optimal too

Many thanks in advance for you suggestions

Hey there.

There’s not really an optimal solution that we support – we expect that on the main branch, all tests will be run in order to provide coverage data.

I suppose you could try and persist coverage reports from previous analyses of the main branch so that they will be used (as long as the new ones are used instead for the affected projects).

This user in a similar setup decided to make sure all tests were run on main: