在 SonarQube 服务器上,对于我的所有项目,都在进行扫描,我可以看到 Sonar 指标,但“新代码”和“整体代码”的单元测试覆盖率均显示为 0%。
项目源代码是用 NodeJS/JavaScript 编写的,我需要用 SonarQube 扫描。
请参考附件截图:-
Package.json:-
{
"name": "analytics",
"version": "1.0.0",
"description": "Microservice to handle CRUD operations of analytics",
"main": "server.js",
"directories": {
"test": "test"
},
"scripts": {
"dev": "node -r dotenv/config server.js",
"dev:watch": "nodemon --watch ../analytics --watch ../../common -r dotenv/config server.js",
"start": "node server.js",
"test": "nyc --reporter=lcov --reporter=text-lcov mocha -r dotenv/config"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.752.0",
"aws-xray-sdk": "^2.5.0",
"axios": "^0.19.2",
"bcrypt": "3.0.8",
"body-parser": "1.19.0",
"cls-hooked": "4.2.2",
"cors": "2.8.5",
"dotenv": "8.2.0",
"express": "4.17.1",
"express-mung": "^0.5.1",
"express-useragent": "^1.0.13",
"fast-csv": "^4.1.3",
"json2csv": "^5.0.5",
"jsonwebtoken": "^8.5.1",
"kafka-node": "^5.0.0",
"lodash": "^4.17.20",
"moment": "^2.24.0",
"multer": "^1.4.2",
"node-cron": "^2.0.3",
"nodemon": "^2.0.5",
"passport-saml": "1.3.3",
"path": "^0.12.7",
"pg": "7.14.0",
"pg-hstore": "2.3.3",
"prom-client": "12.0.0",
"prometheus-api-metrics": "2.2.3",
"qrcode": "1.4.4",
"redis": "^3.0.2",
"redis-clustr": "1.7.0",
"seedrandom": "^3.0.5",
"sequelize": "5.21.2",
"speakeasy": "2.0.0",
"streamifier": "^0.1.1",
"uuid": "3.3.3",
"winston": "3.2.1"
},
"devDependencies": {
"ajv": "6.10.2",
"chai": "4.2.0",
"chai-as-promised": "^7.1.1",
"chai-http": "4.3.0",
"chai-jest-snapshot": "^2.0.0",
"chai-uuid": "1.0.6",
"eslint": "6.8.0",
"mocha": "6.2.2",
"mochawesome": "4.1.0",
"nyc": "14.1.1",
"redis-mock": "0.49.0",
"sequelize-mock": "0.10.2",
"sinon": "7.5.0"
}
}
Jenkinsfile 代码片段:-
stages{
stage('Lint Analysis'){
steps{
echo '*************running lint analysis**********'
// sh 'eslint . --ext .js -c .eslintrc.js --fix || true' //To display output on console
// sh 'eslint . --ext .js -c .eslintrc.js --fix -f checkstyle > eslint.xml || true' //To create report which will be use by checkstyle plugin
}
}
stage('Unit Test'){
steps{
//Running Unit test to get Code Coverage report using clover plugin
dir('services/analytics') {
echo '************ Running UT ***********'
// sh 'npm install'
// sh 'npm run coverage'
// step([
// $class: 'CloverPublisher',
// cloverReportDir: 'test/coverage',
// cloverReportFileName: 'clover.xml',
// healthyTarget: [methodCoverage: 80, conditionalCoverage: 80, statementCoverage: 80],
// unhealthyTarget: [methodCoverage: 50, conditionalCoverage: 50, statementCoverage: 50],
// failingTarget: [methodCoverage: 0, conditionalCoverage: 0, statementCoverage: 0]
// ])
}
}
}
stage('Static Code Analysis by SonarQube') {
steps {
script {
withSonarQubeEnv("aws_sonarqube") {
sh "${tool("sonar_scanner")}/bin/sonar-scanner \
-Dproject.settings=./sonar-project.properties \
-Dsonar.login=$SONAR_TOKEN"
}
}
}
}
}
sonar-project.properties:-
sonar.projectKey=test-analytics
sonar.projectName=test-analytics
sonar.projectVersion=1.0
sonar.language=js
sonar.sources=.
sonar.sourceEncoding=UTF-8
# sonar.exclusions=./**/*.spec.js
sonar.tests=.
sonar.test.inclusions=./**/*.spec.js,./**/*.spec.jsx,./**/*.test.js,./**/*.test.jsx
# sonar.coverage.exclusions=./**/*.spec.js,./**/*.mock.js,node_modules/*,coverage/lcov-report/*
sonar.javascript.lcov.reportPaths=./coverage/lcov.info
# sonar.testExecutionReportPaths=test-report.xml
请指教,谢谢