Don't see Unit Tests count in Dashboard (Node.js & Angular)

  • 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.

Hello Boris,

Welcome to the SonarSource Community, and thank you for your message!

I successfully managed to import your test execution report as it is (except for the file path) as part of analyzing a dummy project. Therefore, would you mind sharing the scanner logs with the debug details? There might be some clues there as to why unit tests count fails to show up in the dashboard.

Last but not least, I see that you are using sonar.typescript.lcov.reportPaths to import your code coverage. Although it shouldn’t have any impact on your problem, please note that this property is deprecated. In that regard, I would kindly suggest you use sonar.javascript.lcov.reportPaths instead.

Regards,
Yassin

Hello Yassin,

Thank you for replying to my question.
As per your suggestion I’ve changed sonar.typescript.lcov.reportPaths to sonar.javascript.lcov.reportPaths.

After this change I ran the report again and, as you mentioned, it didn’t chnage the fact the unit tests count is missing. :slight_smile:

Any way, Please find the SonarQube console output:

[Pipeline] stage
[Pipeline] { (SonarQube)
[Pipeline] script
[Pipeline] {
[Pipeline] withSonarQubeEnv
Injecting SonarQube environment variables using the configuration: Acme SonarQube Server
[Pipeline] {
[Pipeline] echo
Acme SonarQube Server
[Pipeline] echo
http://sonar-dev.acme.com:9000
[Pipeline] echo
******
[Pipeline] sh
+ npm run sonar

> acme@6.0.1 sonar /jenkins-agent/workspace/pos_client_devops-sonarqube-test
> sonar-scanner

[11:44:21] Starting analysis...
[11:44:21] Checking if executable exists: /home/ubuntu/.sonar/native-sonar-scanner/sonar-scanner-4.5.0.2216-linux/bin/sonar-scanner
[11:44:21] Platform binaries for SonarScanner found. Using it.

INFO: Scanner configuration file: /home/ubuntu/.sonar/native-sonar-scanner/sonar-scanner-4.5.0.2216-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/sonar-project.properties
INFO: SonarScanner 4.5.0.2216
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Linux 5.4.0-1029-aws amd64
INFO: User cache: /root/.sonar/cache
INFO: Scanner configuration file: /home/ubuntu/.sonar/native-sonar-scanner/sonar-scanner-4.5.0.2216-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/sonar-project.properties
INFO: Analyzing on SonarQube server 8.9.0
INFO: Default locale: "en_US", source code encoding: "UTF-8"
INFO: Load global settings
INFO: Load global settings (done) | time=97ms
INFO: Server id: xxxxxxxx-xxxxxxx-xxxxxxxxxxxx
INFO: User cache: /root/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=54ms
INFO: Load/download plugins (done) | time=270ms
INFO: Loaded core extensions: developer-scanner

INFO: JavaScript/TypeScript frontend is enabled
INFO: Process project properties
INFO: Process project properties (done) | time=8ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=2ms
INFO: Project key: client-project
INFO: Base dir: /jenkins-agent/workspace/pos_client_devops-sonarqube-test
INFO: Working dir: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/.scannerwork
INFO: Load project settings for component key: 'client-project'
INFO: Load project settings for component key: 'client-project' (done) | time=14ms
INFO: Load project branches
INFO: Load project branches (done) | time=15ms
INFO: Load project pull requests
INFO: Load project pull requests (done) | time=15ms
INFO: Load branch configuration
INFO: Detected branch/PR in 'Jenkins'
INFO: Auto-configuring branch 'devops-sonarqube-test'
INFO: Load branch configuration (done) | time=4ms
INFO: Auto-configuring with CI 'Jenkins'
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=61ms
INFO: Auto-configuring with CI 'Jenkins'
INFO: Load active rules

INFO: Load active rules (done) | time=1155ms
INFO: Branch name: devops-sonarqube-test
INFO: Indexing files...
INFO: Project configuration:
INFO:   Included sources: src/**/*.ts
INFO:   Excluded sources: src/**/*.spec.ts, src/**/*.test.ts, src/**/*.spec.ts, src/**/*.test.ts
INFO:   Included tests: src/**/*.spec.ts, src/**/*.test.ts
INFO: 322 files indexed
INFO: 364 files ignored because of inclusion/exclusion patterns
INFO: 0 files ignored because of scm ignore settings
INFO: Quality profile for ts: All in
INFO: ------------- Run sensors on module client Project
INFO: JavaScript/TypeScript frontend is enabled
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=20ms

INFO: Sensor CSS Rules [cssfamily]
INFO: No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
INFO: Sensor CSS Rules [cssfamily] (done) | time=1ms
INFO: Sensor C# Project Type Information [csharp]
INFO: Sensor C# Project Type Information [csharp] (done) | time=2ms
INFO: Sensor C# Properties [csharp]
INFO: Sensor C# Properties [csharp] (done) | time=0ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=3ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=3ms
INFO: Sensor VB.NET Project Type Information [vbnet]
INFO: Sensor VB.NET Project Type Information [vbnet] (done) | time=1ms
INFO: Sensor VB.NET Properties [vbnet]
INFO: Sensor VB.NET Properties [vbnet] (done) | time=1ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=5ms
INFO: Sensor TypeScript analysis [javascript]

INFO: Deploying custom rules bundle jar:file:/root/.sonar/cache/1c0d0d6d358b2ff25a27998c60079e2e/sonar-securityjsfrontend-plugin.jar!/js-vulnerabilities-rules-1.0.0.tgz to /jenkins-agent/workspace/pos_client_devops-sonarqube-test/.scannerwork/.sonartmp/eslint-bridge-bundle/package/custom-rules8085780776715796122

INFO: Found 2 tsconfig.json file(s): [/jenkins-agent/workspace/pos_client_devops-sonarqube-test/cypress/tsconfig.json, /jenkins-agent/workspace/pos_client_devops-sonarqube-test/tsconfig.json]
INFO: Analyzing 322 files using tsconfig: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/tsconfig.json
INFO: 322 source files to be analyzed
INFO: Load project repositories
INFO: Load project repositories (done) | time=24ms

INFO: 154/322 files analyzed, current file: src/app/_services/intercom.service.ts

INFO: 322/322 source files have been analyzed
INFO: Sensor TypeScript analysis [javascript] (done) | time=16974ms
INFO: Sensor JavaScript/TypeScript Coverage [javascript]
INFO: Analysing [/jenkins-agent/workspace/pos_client_devops-sonarqube-test/reports/jest-coverage/lcov.info]
INFO: Sensor JavaScript/TypeScript Coverage [javascript] (done) | time=102ms
INFO: Sensor ThymeLeaf template sensor [securityjavafrontend]
INFO: Sensor ThymeLeaf template sensor [securityjavafrontend] (done) | time=1ms
INFO: Sensor Generic Test Executions Report
INFO: Parsing /jenkins-agent/workspace/pos_client_devops-sonarqube-test/reports/jestSonar-unit-test-reporter.xml
INFO: Imported test execution data for 0 files
INFO: Test execution data ignored for 108 unknown files, including:
/jenkins-agent/workspace/pos_client_devops-sonarqube-test/src/app/tax-suite/common/services/tax-payer-resolver.guard.spec.ts
/jenkins-agent/workspace/pos_client_devops-sonarqube-test/src/app/_services/pricing.guard.spec.ts
/jenkins-agent/workspace/pos_client_devops-sonarqube-test/src/app/main/content/choose-package/sure-prep-packages/sure-prep-packages.component.spec.ts
/jenkins-agent/workspace/pos_client_devops-sonarqube-test/src/app/tax-suite/common/services/user.service.spec.ts
/jenkins-agent/workspace/pos_client_devops-sonarqube-test/src/app/main/content/workspace/workspace.component.spec.ts
INFO: Sensor Generic Test Executions Report (done) | time=102ms
INFO: Sensor JavaSecuritySensor [security]
INFO: Reading type hierarchy from: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/.scannerwork/ucfg2/java
INFO: Read 0 type definitions
INFO: Reading UCFGs from: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/.scannerwork/ucfg2/java
INFO: No UCFGs have been included for analysis.
INFO: Sensor JavaSecuritySensor [security] (done) | time=4ms
INFO: Sensor CSharpSecuritySensor [security]
INFO: Reading type hierarchy from: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/ucfg_cs2
INFO: Read 0 type definitions
INFO: Reading UCFGs from: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/ucfg_cs2
INFO: No UCFGs have been included for analysis.
INFO: Sensor CSharpSecuritySensor [security] (done) | time=0ms
INFO: Sensor PhpSecuritySensor [security]
INFO: Reading type hierarchy from: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/.scannerwork/ucfg2/php
INFO: Read 0 type definitions
INFO: Reading UCFGs from: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/.scannerwork/ucfg2/php
INFO: No UCFGs have been included for analysis.
INFO: Sensor PhpSecuritySensor [security] (done) | time=0ms
INFO: Sensor PythonSecuritySensor [security]
INFO: Reading type hierarchy from: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/.scannerwork/ucfg2/python
INFO: Read 0 type definitions
INFO: Reading UCFGs from: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/.scannerwork/ucfg2/python
INFO: No UCFGs have been included for analysis.
INFO: Sensor PythonSecuritySensor [security] (done) | time=0ms
INFO: Sensor JsSecuritySensor [security]
INFO: Reading type hierarchy from: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/.scannerwork/ucfg2/js
INFO: Read 0 type definitions
INFO: Reading UCFGs from: /jenkins-agent/workspace/pos_client_devops-sonarqube-test/.scannerwork/ucfg2/js
INFO: No UCFGs have been included for analysis.
INFO: Sensor JsSecuritySensor [security] (done) | time=0ms
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=1ms
INFO: CPD Executor 30 files had no CPD blocks
INFO: CPD Executor Calculating CPD for 292 files
INFO: CPD Executor CPD calculation finished (done) | time=151ms
INFO: Load New Code definition
INFO: Load New Code definition (done) | time=9ms
INFO: Analysis report generated in 182ms, dir size=1 MB
INFO: Analysis report compressed in 639ms, zip size=864 KB
INFO: Analysis report uploaded in 74ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://sonar-dev.acme.com:9000/dashboard?id=client-project&branch=devops-sonarqube-test
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://sonar-dev.acme.com:9000/api/ce/task?id=AXq-lTJOU9-hzefnMQo3

INFO: Analysis total time: 28.422 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 30.149s
INFO: Final Memory: 20M/70M
INFO: ------------------------------------------------------------------------
[11:44:52] Analysis finished.
[Pipeline] }

[Pipeline] // withSonarQubeEnv
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage

Thanks,
Boris.

Hello again,

Thank you for the logs, it helped a lot.

It turns out that there is a typo in your sonar-project.properties. You need to replace sonar.test=src with sonar.tests=src. In other words, the property name is plural.

Hope this helps,
Yassin

Unbelievable!
All the time I spent on this issue, and it all came down to one missing letter! :exploding_head:

BTW, what did you see in the logs that helped you figured out my typo?

Thank you Yassin! :smiley:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.