I have a typescript project that goes through a Jenkins pipeline and does all the functional tests in parallel (after building the main container). At the end of the pipeline - we create code coverage check and then ship the result out to sonarqube.
here is my package.json:
"test": "npm run test:unit && npm run test:component && npm run test:functional",
"test:component": "mocha --reporter mocha-sonarqube-reporter --reporter-options output=tests/coverage/component/test-xcomponent.xml --recursive -r ts-node/register tests/component/*.ts",
"test:functional": "mocha --reporter mocha-sonarqube-reporter --reporter-options output=tests/coverage/functional/test-xfunctional.xml --recursive -r ts-node/register tests/functional/*.ts",
"test:unit": "mocha --reporter mocha-sonarqube-reporter --reporter-options output=tests/coverage/unit/test-xunit.xml --recursive -r ts-node/register tests/unit/*.ts",
"test:unit:nosq": "mocha --recursive -r ts-node/register tests/unit/*.ts",
"lint": "tslint -t verbose --project tsconfig.json -c tslint.json",
"cover": "nyc --report-dir tests/coverage/all npm run test",
"cover:unit": "nyc --report-dir tests/coverage/unit npm run test:unit",
"cover:functional": "nyc --report-dir tests/coverage/functional -x 'app/repositories' -x 'app/entities' -x 'app/utils' --no-clean npm run test:functional"
my sonar-project.properties is like the following:
sonar.exclusions=**/node_modules/**,**/*.spec.ts,app/entities/**,dependency-check-report/*,tests/coverage/**/*
sonar.tests=tests
sonar.test.inclusions=tests/**/*
sonar.ts.tslintconfigpath=tslint.json
sonar.typescript.lcov.reportPaths=tests/coverage/all/lcov.info
I have two problems with this setup:
I cannot seem to find a way to merge the different coverage files. I have checked the official istanbuljs/nyc GitHub and it states that it can combine via nyc merge
command however the output is .json and sonarqube requires xml. I am stuck with half my code coverage since I'm only shipping out one file out and not the combine file.
I currently have code smell errors from the tests/coverage/all folder as it considered missing fonts from the generated coverage is being missing. I have excluded that folder in the sonar-project.properties
file and I've included it in the .gitignore too but sonarqube is still reporting it as code small.
Copy your code coverage results in SonarQube format to a directory, for example, C:\MyCodeCoverageResults. In that directory, run the SonarScanner. In the command output, locate the ANALYSIS SUCCESSFUL line, and copy the HTTP link. Paste the link to a browser.
Test coverage reports describe the percentage of your code that has been tested by your test suite during a build. This differs from test execution reports, which describe which tests within your test suite have been run during a build.
SonarQube does not require XML files for JavaScript coverage, it requires the report to be in lcov format. Please see SonarQube's documentation: JavaScript Coverage Results Import.
In order to generate this lcov report, you can do the following:
__coverage__
global to) in a directory, the default is .nyc_output
nyc report --reporter=lcov --report-dir=.nyc_coverage
nyc
that you want to generate a report using all the files in the directory specified by --report-dir
(.nyc_coverage
in this case) and that you want the report in the format specified by --reporter
(lcov
in this case)nyc
will create a folder (.nyc_output
by default) and write the lcov file thereIf you would like, you can also add extra reporters for sanity. I usually add --reporter=text
so that it will print out the coverage as well.
So your final command might be:
nyc report \
--reporter=lcov \
--reporter=text \
--report-dir=.nyc_coverage
The =
are optional and the command arguments can precede the sub-command, so you can also run the command that you noted:
nyc --reporter lcov --reporter text --report-dir .nyc_coverage report
Additionally, you tell SonarQube where the report is by specifying it on command line:
sonar-scanner \
-Dsonar.projectKey=whatever \
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
Or you can set it in the project settings:
Project -> Administration -> JavaScript -> Tests and Coverage -> LCOV Files
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With