Jest + SonarQube - 报告未导入

fer*_*rds 5 node.js typescript sonarqube sonarqube-scan ts-jest

我在 NodeJS + Typescript 应用程序上运行声纳扫描仪,并使用 Jest 进行单元测试。我能够在控制台上看到报告,显示创建的每个模块的覆盖率。然后我查找了如何使用 jest-sonar-reporter 将 Jest 与 Sonarqube 集成,它按预期生成 report.xml。

我无法获得运行 jet --coverage 的覆盖范围以显示在 sonarqube 中。

这是我项目的结构

??? 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
Run Code Online (Sandbox Code Playgroud)

我的 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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的 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"
  }
}
Run Code Online (Sandbox Code Playgroud)

我的声纳-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
Run Code Online (Sandbox Code Playgroud)

这些是 jest --coverage 和 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.
Run Code Online (Sandbox Code Playgroud)

如下所示,它表示已成功找到并解析了覆盖率报告,但我在 Sonar 仪表板上看不到它:

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
Run Code Online (Sandbox Code Playgroud)

这是报告的内容:

<?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>
Run Code Online (Sandbox Code Playgroud)

$sonar-scanner -X

在这里输出

编辑 1 SonarQube 版本 - 版本是 - Community EditionVersion 7.9.1 (build 27448)