I'm running sonar-scanner on my NodeJS + Typescript app, as well as using Jest for the unit test. I'm able to see the report on the console, displaying the coverage for each of the modules created. I then looked up how to integrate Jest with Sonarqube using jest-sonar-reporter, which is generating the report.xml as expected.
I'm having trouble getting the coverage which I get running jet --coverage to be displayed in sonarqube.
This is the structure of my project
├── babel.config.js
├── coverage
│ ├── clover.xml
│ ├── coverage-final.json
│ ├── lcov.info
│ ├── lcov-report
│ └── test-reporter.xml
├── Dockerfile
├── ecs-meta.json
├── Jenkinsfile
├── nodemon.json
├── package.json
├── package-lock.json
├── properties
│ ├── application-dev.json
│ ├── application-prd.json
│ ├── application-tst.json
│ └── application-uat.json
├── README.md
├── sonar-project.properties
├── sonarqube.ts
├── src
│ ├── configs
│ ├── controllers
│ │ └── StoreController.ts
│ ├── index.ts
│ ├── models
│ │ ├── consul.model.ts
│ │ └── UserModel.ts
│ ├── routes
│ │ ├── index.ts
│ │ └── StoreRoutes.ts
│ ├── services
│ │ ├── __mocks__
│ │ │ └── Repository.ts
│ │ └── Repository.ts
│ ├── test
│ │ ├── controllers
│ │ │ └── index.test.ts
│ │ └── utils
│ │ └── ExpressResponseMock.ts
│ └── typings.d.ts
├── tsconfig.json
└── tslint.json
My tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
}
},
"include": [
"src"
]
}
My package.json
{
"name": "core",
"version": "1.0.0",
"scripts": {
"sonar": "npx sonar-scanner",
"test": "jest --collectCoverage",
"start": "echo \"== DO NOT USE START SCRIPT FOR PRODUCTION ==\" && nodemon",
"build": "tsc",
"lint": "tslint --project ./tsconfig.json --config ./tslint.json",
"review": "cross-env CI=true && npm run lint && npm run build",
"prettify": "prettier --write ./src/**/*.ts"
},
"jest": {
"testEnvironment": "node",
"collectCoverage": true,
"verbose": true,
"modulePathIgnorePatterns": [
"src/config/*"
],
"collectCoverageFrom": [
"src/**/*.{ts,tsx}"
],
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"testResultsProcessor": "jest-sonar-reporter"
},
"jestSonar": {
"reportPath": "coverage",
"reportFile": "test-reporter.xml",
"indent": 4
},
"husky": {
"hooks": {
"pre-commit": "lint-staged",
"pre-push": "npm run review"
}
},
"lint-staged": {
"*.{js,ts}": [
"npm run lint",
"npm run prettify"
]
},
"prettier": {
"printWidth": 110
},
"dependencies": {
"@types/axios": "^0.14.0",
"@types/consul": "^0.23.34",
"@types/express": "^4.17.6",
"@types/jest": "^26.0.15",
"@types/node": "^13.5.1",
"@types/request": "^2.48.5",
"axios": "^0.19.0",
"body-parser": "^1.19.0",
"consul": "^0.37.0",
"cross-env": "^7.0.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jest": "^26.6.0",
"node-vault": "^0.9.20",
"nodemon": "^2.0.2",
"request": "^2.88.2",
"ts-node": "^8.6.2",
"tslint": "^6.0.0",
"typescript": "^3.9.7",
"vault-auth-aws": "^0.1.7"
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"@babel/preset-typescript": "^7.12.1",
"babel-jest": "^26.6.0",
"husky": "^4.2.5",
"jest-sonar-reporter": "^2.0.0",
"lint-staged": "^10.2.9",
"prettier": "^2.0.5",
"pretty-quick": "^2.0.1",
"sonarqube-scanner": "^2.7.0",
"supertest": "^5.0.0",
"ts-jest": "^26.4.1",
"tslint-config-prettier": "^1.18.0"
}
}
My sonar-project.properties
#sonar.organization=YOUR-ORGANISATION-KEY
# relative paths to source directories. More details and properties are described
# in https://sonarcloud.io/documentation/project-administration/narrowing-the-focus/
sonar.host.url=https://sonarqube.in
sonar.projectKey=**
sonar.sources=src
sonar.tests=src/test
sonar.test.inclusions=src/**/*.spec.js,src/**/*.spec.jsx,src/**/*.test.js,src/**/*.test.jsx,src/**/*.test.ts,src/**/*.test.tsx
sonar.javascript.lcov.reportPaths=./coverage/lcov.info
sonar.testExecutionReportPaths=./coverage/test-reporter.xml
sonar.sourceEncoding=UTF-8
These are outputs for both jest --coverage, and sonar-scanner:
$jest --coverage
PASS src/test/controllers/index.test.ts
Get
✓ Get Stores (12 ms)
console.log
Starting lookup for store ""
at Object.getStores (src/controllers/StoreController.ts:4:11)
console.info
Success retrieving store based on "" details!
at then.result (src/controllers/StoreController.ts:7:15)
-------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------------|---------|----------|---------|---------|-------------------
All files | 17.24 | 0 | 33.33 | 17.24 |
src | 0 | 0 | 0 | 0 |
index.ts | 0 | 0 | 0 | 0 | 8-28
typings.d.ts | 0 | 0 | 0 | 0 |
src/controllers | 66.67 | 100 | 66.67 | 66.67 |
StoreController.ts | 66.67 | 100 | 66.67 | 66.67 | 11-12
src/models | 0 | 0 | 0 | 0 |
UserModel.ts | 0 | 0 | 0 | 0 |
consul.model.ts | 0 | 0 | 0 | 0 |
src/routes | 0 | 100 | 0 | 0 |
StoreRoutes.ts | 0 | 100 | 100 | 0 | 3-5
index.ts | 0 | 100 | 0 | 0 | 5-14
src/services | 0 | 0 | 0 | 0 |
Repository.ts | 0 | 0 | 0 | 0 | 3-16
src/test/utils | 100 | 100 | 100 | 100 |
ExpressResponseMock.ts | 100 | 100 | 100 | 100 |
-------------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 1.995 s
Ran all test suites.
As can be seen below, it says that the coverage report was found and parsed successfully, but I do not see it on Sonar dashboard:
INFO: Parsing /mnt/86da7b30-23bc-4ecb-a95e-bbeaecd50c7d/Documents/work/projects/stores/coverage/test-reporter.xml
INFO: Imported test execution data for 1 files
INFO: Sensor Generic Test Executions Report (done) | time=16ms
This is the content of the report:
<?xml version="1.0" encoding="UTF-8"?>
<testExecutions version="1">
<file path="/mnt/86da7b30-23bc-4ecb-a95e-bbeaecd50c7d/Documents/work/projects/stores/src/test/controllers/index.test.ts">
<testCase name="Get Get Stores" duration="12"/>
</file>
</testExecutions>
$sonar-scanner -X
from Jest + SonarQube - Report not being imported
No comments:
Post a Comment