SonarCloud is used to gather quality metrics, including static code analysis for Javascript.
In order to report unit test code coverage back to SonarCloud, we need to use the Chrome Debugger Protocol to extract coverage data, and transform it to SonarCloud's expected Generic Report Format.
- checkout the repository
- generate a certificate
- run a
nginx
server with the certificate onhttps://localhost/todos
- install
chromium-browser
,jq
,curl
andwebsocat
- run
chromium-browser
in headless mode and instruct over the Chrome Debugger Protocol web socket to collect coverage after navigating to the unit test documents. - transform Chrome coverage format to SonarCloud Generic Coverage Report format with
transform-coverage.js
- run SonarCloud Scan with coverage report path configured
- Install SonarCloud Scan Github action on repository
- Create project in SonarCloud
- Add
SONAR_TOKEN
from https://sonarcloud.io/account/security/ as a repository action secret - Configure action arguments in sonarcloud.yml with values from the SonarCloud project dashboard
-Dsonar.organization=tmf -Dsonar.projectKey=tmf_todos
See A quick look at how Chrome's JavaScript code coverage feature works
Chrome Debugger Protocol messages used:
{"id":1,"method":"Profiler.enable"}
{"id":3,"method":"Profiler.startPreciseCoverage","params":{"callCount":true,"detailed":true,"allowTriggeredUpdates":false}}
{"id":4,"method":"Page.navigate","params":{"url":"https://localhost/todos/classes/offline-cache.html"}}
{"id":5,"method":"Profiler.takePreciseCoverage"}