jest 和 nyc 的集成测试的代码覆盖率始终返回 0% 的语句和行

问题描述 投票:0回答:1

我正在尝试为我们的

Jest
集成测试做代码覆盖。 测试目录位于服务器项目中的某个子目录中,具有独立的
package.jso
n 和配置。 需要覆盖的服务和库位于主项目根目录中,即在测试子目录之外。

当我通过

nyc
尝试时,它能够在测试目录之外包含所需的目录,但报告中没有返回真正的覆盖范围。

当我尝试通过

--coverage
中的
Jest
标志进行标识时,即使在配置文件中添加
rootDir
选项,它也无法访问测试目录之外的内容,这只会让项目变得一团糟。

我也尝试过执行

nyc
以及 api-gateway 启动和服务启动,但它也没有改变结果。

项目架构如下-->

server-project root
  .nyc_output
  _coverage_
  services
  libs
  src
  api-gateway
  test
  - src
   - integration-tests
     -  test.ts
     -  ...
  - jest.config.ts
  - jest-integration.config.ts
  - package.json
  package.json
  

相关版本:

Node v14.18.1
npm 6.14.15
pnpm 6.9.1

"istanbul-reports": "^3.1.5",
"jest": "^26.6.3",
"@types/jest": "^26.0.18",
"@types/node": "13.7.1",
"babel-plugin-istanbul": "^6.1.1",
"nyc": "^15.1.0",
"ts-jest": "^26.4.4",
"ts-node": "^9.1.1",
"typescript": "^4.1.2"

下面的脚本包含 nyc 和 jest - 它们都没有按预期工作:

"test:integration:coverage": "nyc --cwd='../' --exclude-after-remapjest=false --reporter=lcov --reporter=text pnpm run test:integration -- --coverage --collectCoverageFrom='./../services/**/src/*.ts'",

它位于

test/package.json
里面,由
pnpm

发射

nyc
里面的配置
package.json
(项目根目录)-->

  "nyc": {
    "extends": "@istanbuljs/nyc-config-typescript",
    "require": "@babel/register",
    "check-coverage": true,
    "all": true,
    "cache": true,
    "sourceMap": false,
    "instrument": true,
    "lines": 90,
    "branches": 90,
    "functions": 90,
    "statements": 90,
    "reporter": [
      "text-summary",
      "lcov",
      "text"
    ],
    "report-dir": "coverage",
    "include": [
      "services/**/src/*.ts",
      "api-gateway/src/*.ts",
      "libs/**/src/*.ts"
    ],
    "exclude": [
      "**/test/**",
      "**/*.d.ts"
    ],
    "extension": [
      ".ts",
      ".tsx"
    ],
    "temp-dir": ".nyc_output",
    "excludeNodeModules": true,
    "source-map": true,
    "produce-source-map": true
  }

Jest 配置在旁边

test/jest-integration.config.ts
(扩展到主 jest.config.ts 文件)-->

    // eslint-disable-next-line @typescript-eslint/no-var-requires
const baseConfig = require('./jest.config');
const base = '**/integration-tests/services';
const setupDir = './src/tests/integration-tests/setup';
module.exports = {
  ...baseConfig,
  testMatch: [
    ...[
      'abtests/*.test.ts',
       'moretests/*.test.ts',
  ],
  globalSetup: `${setupDir}/jest-integration-setup.ts`,
  globalTeardown: `${setupDir}/jest-integration-teardown.ts`,
};

输出结果:

  at src/tests/integration-tests/services/abtests/abtest.test.ts:18:13

通过 src/tests/integration-tests/services/abtests/abtest.test.ts 反对 ✓ 禁止我查询中的参数(300 毫秒) ✓ 覆盖现有测试(110 毫秒) ✓ 创建一个具有价值的新测试(109 毫秒)

--------|--------|--------|--------|-------- --|-------------------- 文件 | % Stmts | % 分支 | % 函数 | % 行 |未覆盖的行#s ----------|--------|----------|--------|-------- -|-------------------- 所有文件 | 0 | 0 | 0 | 0 |
----------|--------|----------|--------|-------- -|--------------------

    Failed to write coverage reports:
    ERROR: TypeError: Cannot convert undefined or null to object
    STACK: TypeError: Cannot convert undefined or null to object
at Function.getPrototypeOf (<anonymous>)
at Date (/Users/myname/repos/server-project/node_modules/.pnpm/[email protected]/node_modules/jest-date-mock/lib/mockDate.js:39:44)
at new HtmlReport (/Users/myname/repos/server-project/node_modules/.pnpm/[email protected]/node_modules/istanbul-reports/lib/html/index.js:260:21)
at new LcovReport (/Users/myname/repos/server-project/node_modules/.pnpm/[email protected]/node_modules/istanbul-reports/lib/lcov/index.js:14:21)
at Object.create (/Users/myname/repos/server-project/node_modules/.pnpm/[email protected]/node_modules/istanbul-reports/index.js:22:16)
at /Users/myname/repos/server-project/node_modules/.pnpm/@[email protected]/node_modules/@jest/reporters/build/CoverageReporter.js:248:20
at Array.forEach (<anonymous>)
at CoverageReporter.onRunComplete (/Users/myname/repos/server-project/node_modules/.pnpm/@[email protected]/node_modules/@jest/reporters/build/CoverageReporter.js:240:25)
at ReporterDispatcher.onRunComplete (/Users/myname/repos/server-project/node_modules/.pnpm/@[email protected]/node_modules/@jest/core/build/ReporterDispatcher.js:88:9)
at TestScheduler.scheduleTests (/Users/myname/repos/server-project/node_modules/.pnpm/@[email protected]/node_modules/@jest/core/build/TestScheduler.js:350:5)
at runJest (/Users/myname/repos/server-project/node_modules/.pnpm/@[email protected]/node_modules/@jest/core/build/runJest.js:376:19)
at _run10000 (/Users/myanme/repos/server-project/node_modules/.pnpm/@[email protected]/node_modules/@jest/core/build/cli/index.js:416:7)
at runCLI (/Users/myname/repos/server-project/node_modules/.pnpm/@[email protected]/node_modules/@jest/core/build/cli/index.js:271:3)
at Object.run (/Users/myname/repos/server-project/node_modules/.pnpm/[email protected]/node_modules/jest-cli/build/cli/index.js:163:37)
  

测试套件:2 个通过,总共 2 个 测试:跳过 1 次,通过 4 次,总共 5 次 快照:共 0 个 时间:10.01 秒 运行所有测试套件。 📦 报告创建于:/Users/myname/repos/server-project/test/html-report/report.html 错误:线路覆盖率 (0%) 未达到全局阈值 (90%) 错误:功能覆盖率 (0%) 未达到全局阈值 (90%) 错误:分支机构的覆盖率 (0%) 未达到全球阈值 (90%) 错误:报表的覆盖率 (0%) 未达到全球阈值 (90%) --------------------------------------|-------- -|------------|--------|--------|---------------- -- 文件 | % Stmts | % 分支 | % 函数 | % 行 |未覆盖的行#s --------------------------------------|-------- -|------------|--------|--------|---------------- -- 所有文件 | 0 | 0 | 0 | 0 |
api-网关/src | 0 | 0 | 0 | 0 |
配置.ts | 0 | 100 | 100 | 0 | 81
主.ts | 0 | 0 | 0 | 0 | 11-43
存储.ts | 0 | 0 | 0 | 0 | 6-19
libs/apollo-federation-tester/src | 0 | 0 | 0 | 0 |
索引.ts | 0 | 0 | 0 | 0 | 17-85

请您帮忙! 谢谢。

jestjs integration-testing code-coverage ts-jest nyc
1个回答
0
投票

我在这里遇到同样的问题。似乎没有跟踪在 globalSetup 上执行的模块的开玩笑覆盖率。

© www.soinside.com 2019 - 2024. All rights reserved.