- Which versions are you using?
- SonarQube: Developer Edition 8.9.0.43852
- Scanner: sonarqube-scanner 2.8.0
- Display Unit Test info
Hi,
We’ve started using SonarQube last week and we have a problem viewing the Unit Test report inside SonarQube.
We incorporated SonarQube as a stage in our Jenkins pipeline and the basic setup is working.
But after adding the Unit Test report, we don’t see it in SonarQube’s dashboard.
Our setup:
We use jest
for our unit tests.
package.json (redacted)
{
"name": "transactions",
"version": "0.0.1",
"description": "",
"private": true,
"main": "./src/bin/www.js",
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"lint:ci": "eslint . --ext .ts --format json -o reports/eslint/js-lint-results.json",
"test:unit:ci": "node --max_old_space_size=4096 ./node_modules/jest/bin/jest.js -c ./jest.config.unit.js --no-cache --ci --logHeapUsage --collectCoverage --runInBand --coverage",
"sonar": "sonar-scanner"
},
"dependencies": {
...
},
"devDependencies": {
"@types/jest": "^26.0.22",
"@types/node": "^10.12.30",
"@typescript-eslint/eslint-plugin": "^3.6.1",
"@typescript-eslint/parser": "^3.6.1",
"eslint": "^7.5.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.6.3",
"jest-junit": "^10.0.0",
"lint-staged": "^10.0.7",
"prettier": "^1.19.1",
"ts-jest": "^26.5.5",
"ts-node-dev": "^1.0.0-pre.31",
"tslint": "^5.13.1",
"typescript": "^3.8.3",
"sonarqube-scanner": "^2.8.0",
"eslint-plugin-sonarjs": "^0.7.0",
"jest-sonar-reporter": "^2.0.0"
},
"lint-staged": {
"*.{ts}": [
"./node_modules/.bin/prettier --write",
"./node_modules/.bin/eslint --fix",
"git add"
],
"*.{js,json}": [
"./node_modules/.bin/prettier --write",
"git add"
]
},
"jestSonar": {
"reportPath": "reports",
"reportFile": "jestSonar-unit-test-reporter.xml",
"indent": 4
}
}
jest.config.unit.js
module.exports = {
roots: ['src/'],
testMatch: ['**/*.(test|spec).[jt]s'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest'
},
testEnvironment: 'node',
reporters: ['default', 'jest-junit'],
setupFilesAfterEnv: ['./__tests__/jest.setup.js'],
collectCoverage: true,
testResultsProcessor: 'jest-sonar-reporter',
collectCoverageFrom: [
'<rootDir>/src/**/*.ts',
'<rootDir>/src/**/*.spec.ts',
'<rootDir>/src/**/*.test.ts',
'!<rootDir>/src/public/*.ts',
'!<rootDir>__mocks__/*.ts',
'!<rootDir>/src/**/FileTypeHandler/inputs.ts',
'!<rootDir>/src/**/ratesMock*.ts',
'!<rootDir>/src/**/*.interface.ts',
'!<rootDir>/src/**/*.mock.ts',
'!<rootDir>/src/**/*.module.ts',
'!<rootDir>/src/**/*.d.ts'
],
coverageDirectory: 'reports/jest-coverage/unit',
coverageReporters: ['text', 'cobertura', 'lcov']
};
sonar-project.properties
# Project General Settings
sonar.projectKey=server-name-project
sonar.projectName=Server Name Project
sonar.projectVersion=1.0
sonar.language=ts
sonar.sourceEncoding=UTF-8
# Source File Inclusions/Exclusions
sonar.sources=src
sonar.inclusions=src/**/*.ts
sonar.exclusions=src/**/*.spec.ts, src/**/*mock*.ts, src/**/*Mock*.ts, src/**/*.test.ts, src/**/*.snap, src/docs/**/*, src/recordings/**/*, src/public/**/*
# Test File Inclusions/Exclusions
sonar.test=src
sonar.test.inclusions=src/**/*.spec.ts, src/**/*.test.ts
# Linter
sonar.ts.tslintconfigpath=.eslintrc.json
sonar.eslint.reportPaths=reports/eslint/ts-lint-results.json
# TypeScript Code covarge
sonar.typescript.lcov.reportPaths=reports/jest-coverage/unit/lcov.info
sonar.testExecutionReportPaths=reports/jestSonar-unit-test-reporter.xml
# Scanner CLI Loggin Settings
#sonar.log.level=DEBUG
#sonar.verbose=TRUE
Jenkins SonarQube Stages (partial and redacted)
stage('Install Dependencies') {
steps {
script {
// ...
sh 'npm ci'
}
}
}
stage('Linter') {
steps {
script {
// ...
sh 'npm run lint:ci'
}
}
}
stage('Unit Tests') {
steps {
script {
// ...
sh 'npm run test:unit:ci'
}
}
}
stage('SonarQube') {
steps {
script {
withSonarQubeEnv('SonarQube Server') {
println(env.SONAR_CONFIG_NAME)
println(env.SONAR_HOST_URL)
println(env.SONAR_AUTH_TOKEN)
sh "npm run sonar"
}
}
}
}
Using the above setup, the lint report is added to the Issues and we see the Coverage percentages and able to see in dept coverage drill down.
The issue we have is:
We don’t see Unit Test information.
The unit test report is defined here: sonar.testExecutionReportPaths=reports/jestSonar-unit-test-reporter.xml
and the file jestSonar-unit-test-reporter.xml
is been generated and it has the following basic structure:
<?xml version="1.0" encoding="UTF-8"?>
<testExecutions version="1">
<file path="/jenkins-agent/workspace/pos_server-sonarqube-conf/src/services/transactions/TrUtils 2.0/TrResultsMapper.spec.ts">
<testCase name="map userTransaction ActionType should map userTransaction to be with comment: 8 and status: 1" duration="4"/>
<testCase name="map userTransaction ActionType should map userTransaction to be with comment: 8 and status: 1 on userTransaction" duration="2"/>
</file>
</testExecutions>
Please look at the screenshot.
Thank you,
Boris.