【发布时间】:2021-06-10 12:49:56
【问题描述】:
我正在为我的 Nestjs 项目使用 Sonarqube 分析。当我开玩笑地运行单元测试时,它们都通过了,代码覆盖率约为 80%。在 Sonarqube 上,它仍然显示为 0%。
我的sonar-project.properties文件如下
sonar.projectKey=<project-key>
sonar.projectName=
sonar.sources=src
sonar.tests=src
sonar.inclusions=**
sonar.test.inclusions=src/**/*.spec.ts
sonar.testExecutionReportPaths=test-report.xml
sonar.exclusions=node_modules
我的jest.json如下
{
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "/src/.*\\.(test|spec).(ts|tsx|js)$",
"collectCoverageFrom" : ["src/**/*.{js,jsx,tsx,ts}", "!**/node_modules/**", "!**/vendor/**"],
"coverageReporters": ["json", "lcov"]
}
我的 package.json 的相关部分
"devDependencies": {
"@nestjs/cli": "^7.5.4",
"@nestjs/schematics": "^7.2.7",
"@nestjs/testing": "^7.6.11",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.25",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.1.4",
"jest": "^26.6.3",
"jest-sonar-reporter": "^2.0.0",
"prettier": "^2.2.1",
"serverless": "^2.23.0",
"supertest": "^6.1.3",
"ts-jest": "^26.5.0",
"ts-loader": "^8.0.15",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.1.3"
},
"jest": {
"testResultsProcessor": "jest-sonar-reporter",
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
我的源代码及其各自的单元测试位于同一位置。测试文件的扩展名为 .spec.ts
在本地运行测试时,所有测试都通过,并且在本地 repo 上生成 test-report.xml。我们还在运行声纳扫描仪之前在 Jenkins 上运行单元测试。
Sonarqube 代码覆盖率对我们来说是一个重要指标,没有它,管道就不会进一步移动。这里有什么提示吗?
Sonarqube 企业版版本 7.9.5(内部版本 38598)
【问题讨论】:
标签: typescript unit-testing sonarqube nestjs