测试覆盖率:未覆盖导入语句

Test coverage: import statements not covered

我正在使用 Jest 为打字稿中的 Nest 应用程序编写测试。问题是,我发现导入语句说“else path not taken”和“branch not covered”。未覆盖的导入因 类 而异。有时它在一行的中间似乎是随机的下划线,请参见下面的屏幕截图。因此,总体分支机构覆盖率仅为 47%,其中一条进口线未被覆盖。

生成使用不同报告器的 SonarQube 报告时,问题仍然存在。

有时它甚至适用于注释。

我已经试过了:

  1. 按照概述 sourceMap = true 切换
  2. 删除转换并将 moduleDirectories 添加到 package.json,如 所述。
  3. 将 Jest rootDir 移动到项目根目录中。

没有任何帮助。我完全被这个问题所困扰,特别是因为它是许多项目中使用的默认设置。看起来有些东西与其中一个配置文件有关。知道出了什么问题吗?如何修复那些未覆盖的导入?

以下是一些项目配置:

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": false,
    "outDir": "./dist",
    "baseUrl": "./src",
    "incremental": false,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "noImplicitAny": true,
    "allowUnreachableCode": false,
    "strict": true,
    "alwaysStrict": true,
    "strictPropertyInitialization": false,
    "noUnusedLocals": true,
    "noUnusedParameters": true
  }
}

jest.config.ts

/** @format */

module.exports = {
  moduleFileExtensions: ['js', 'json', 'ts'],
  testRegex: '.*\.spec\.ts$',
  transform: {
    '^.+\.(t|j)s$': 'ts-jest',
  },
  collectCoverageFrom: [
    '**/*.(t|j)s',
    '!generated/openapi/model/*',
    '!types/*',
    '!generate-typings.ts',
  ],
  rootDir: './src',
  coverageDirectory: '../coverage',
  testEnvironment: 'node',
  reporters: ['default', 'jest-junit'],
  setupFilesAfterEnv: ['../test/jest.setup.redis-mock.ts'],
};

package.json

{
  "name": "",
  "version": "0.0.0-local",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "postinstall": "npm run generate",
    "prebuild": "npm run clearDist",
    "clearDist": "rimraf ./dist",
    "generate": "rimraf ./src/generated && npm run generate:graphql && npm run generate:openapi",
    "generate:graphql": "mkdirp ./src/generated && ts-node ./src/generate-typings.ts",
    "generate:openapi": "mkdirp ./src/generated && openapi-generator-cli generate",
    "build": "nest build",
    "build:prod": "nest build --webpack",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "cross-env GRAPHQL_PLAYGROUND=true cross-env NODE_ENV=development nest start --watch",
    "start:debug": "cross-env NODE_ENV=development nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\"",
    "lint:fix": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest --runInBand",
    "test:ci": "jest --coverage --runInBand",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "test:sonar": "jest --coverage --runInBand --testResultsProcessor jest-sonar-reporter"
  },
  "dependencies": {
    "@apollo/gateway": "^0.50.1",
    "@azure/identity": "^2.0.4",
    "@azure/keyvault-secrets": "^4.4.0",
    "@nestjs/apollo": "^10.0.9",
    "@nestjs/axios": "^0.0.7",
    "@nestjs/common": "^8.4.4",
    "@nestjs/config": "^2.0.0",
    "@nestjs/core": "^8.4.4",
    "@nestjs/graphql": "^10.0.9",
    "@nestjs/passport": "^8.2.1",
    "@nestjs/platform-express": "^8.4.4",
    "@nestjs/schedule": "^1.1.0",
    "@nestjs/typeorm": "^8.0.3",
    "apollo-server-express": "^3.6.7",
    "applicationinsights": "^2.3.1",
    "axios": "^0.26.1",
    "cache-manager": "^3.6.1",
    "cache-manager-redis-store": "^2.0.0",
    "clone": "^2.1.2",
    "graphql": "^16.3.0",
    "graphql-fields-list": "^2.2.4",
    "mysql2": "^2.3.3",
    "openid-client": "^5.1.5",
    "redis": "^3.1.1",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.5.5",
    "ts-morph": "^14.0.0",
    "typeorm": "^0.2.34"
  },
  "jest-junit": {
    "outputDirectory": "./junit-reports"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.2.5",
    "@nestjs/schematics": "^8.0.10",
    "@nestjs/testing": "^8.4.4",
    "@openapitools/openapi-generator-cli": "^2.4.26",
    "@types/cache-manager": "^3.4.3",
    "@types/cache-manager-redis-store": "^2.0.1",
    "@types/clone": "^2.1.1",
    "@types/cron": "^1.7.3",
    "@types/express": "^4.17.13",
    "@types/jest": "^27.4.1",
    "@types/node": "^17.0.25",
    "@types/redis": "^2.8.32",
    "@types/supertest": "^2.0.12",
    "@types/ws": "^8.5.3",
    "@typescript-eslint/eslint-plugin": "^5.20.0",
    "@typescript-eslint/parser": "^5.20.0",
    "cross-env": "^7.0.3",
    "eslint": "^8.13.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "^27.5.1",
    "jest-junit": "^13.0.0",
    "jest-sonar-reporter": "^2.0.0",
    "mkdirp": "^1.0.4",
    "prettier": "^2.6.2",
    "redis-mock": "^0.56.3",
    "rimraf": "^3.0.2",
    "supertest": "^6.2.2",
    "ts-jest": "^27.1.4",
    "ts-loader": "^9.2.8",
    "ts-node": "^10.7.0",
    "tsconfig-paths": "^3.14.1",
    "typescript": "^4.6.3",
    "webpack": "^5.72.0",
    "webpack-cli": "^4.9.2"
  },
  "engines": {
    "npm": "^7",
    "node": "^14"
  }
}

我今天第二次尝试启用 source map,很有趣,它成功了。昨天一定是漏了什么。感谢戴夫再次提出来!

我的理解是 Jest 在 运行 测试 [1] 之前将 TypeScript 代码转换为 JavaScript,这会导致有关代码覆盖率的错误警报,因为无法在原始代码中找到正确的行成立。在 tsconfig.json 中设置 sourceMap=true 解决了我的问题。

但是,我有一个后端应用程序,因此最终用户看不到代码。在前端应用程序中启用源映射时,请注意安全方面的代码 public.