簡體   English   中英

節點打字稿項目中的笑話覆蓋率總是返回空

[英]Jest coverage in a node typescript project always returns empty

當我試圖獲取單元測試用例的覆蓋率報告時,我正在處理后端 Typescript 項目,Jest 在終端和 html 報告中返回空的覆蓋率報告,沒有說明任何內容。 我也試過-- --coverage --watchAll=false但它也返回空文檔。 我無法找到解決此問題的方法。 誰能幫我弄清楚我在這里做錯了什么?

包.json

"scripts":{
    "unit-test": "jest --config='./config/jest/jest_unit.config.js' --forceExit --detectOpenHandles",
}

jest_unit.config.js

/**
 * @file Jest Unit Test Configuration File
 */
module.exports = {
  roots: ['../../tests'],
  testRegex: '.*_unit.test.(js|ts|tsx)?$',
  globals: {
    'ts-jest': {
      tsConfig: 'tsconfig.json',
    },
  },
  moduleFileExtensions: ['ts', 'js'],
  transform: {
    '^.+\\.(ts|tsx)$': 'ts-jest',
  },
  testEnvironment: 'node',
  reporters: [
    'default',
    [
      '../../node_modules/jest-html-reporter',
      {
        pageTitle: 'Unit Test Report',
        outputPath: 'tests/reports/unit-test-report.html',
        includeFailureMsg: true,
      },
    ],
  ],
  collectCoverage: true,
  collectCoverageFrom: [
    '../../api/**/*.ts'
  ],
  moduleFileExtensions: ["ts", "js", "json"],
  coverageDirectory: "../../coverage",
  coveragePathIgnorePatterns: [
    "<rootDir>/node_modules"
  ],
  coverageReporters: [
    "json",
    "lcov",
    "text"
  ],
  coverageThreshold: {
    "global": {
      "branches": 100,
      "functions": 100,
      "lines": 100,
      "statements": 100
    }
  },
};

文件夾結構

|-- app
    |-- controllers
    |-- schemas
|-- config
    |-- jest
        |-- jest_unit.config.js
|-- package.json
|-- tests
    |-- api
        |-- modules
            |-- m1
                |-- controllers
                    |-- m1_controller_unit.test.ts
                    |-- m1_controller_integration.test.ts
            |-- m2
                |-- models
                    |-- m1_model_unit.test.ts
                    |-- m1_model_integration.test.ts
            |-- m3
                |-- schemas
                    |-- m1_schema_unit.test.ts
                    |-- m1_schema_integration.test.ts

Jest Coverage htm 和終端顯示沒有覆蓋線

在此處輸入圖像描述

可以通過將配置文件移動到 root 來解決這個問題,這里的配置一切都很好,我不知道為什么 jest 會這樣。

更新結構

文件夾結構

|-- app
    |-- controllers
    |-- schemas
|-- jest_unit.config.js
|-- package.json
|-- tests
    |-- api
        |-- modules
            |-- m1
                |-- controllers
                    |-- m1_controller_unit.test.ts
                    |-- m1_controller_integration.test.ts
            |-- m2
                |-- models
                    |-- m1_model_unit.test.ts
                    |-- m1_model_integration.test.ts
            |-- m3
                |-- schemas
                    |-- m1_schema_unit.test.ts
                    |-- m1_schema_integration.test.ts

包.json

"scripts":{
    "unit-test": "jest --config='./jest_unit.config.js' --forceExit --detectOpenHandles",
}

嘗試更改您的collectCoverageFrom如下:

  collectCoverageFrom: [
    '../../tests/**'
  ],

**表示遞歸地包含所有文件。 詳細信息位於https://jestjs.io/docs/en/configuration#collectcoveragefrom-array

我也隨機面臨這個問題,我沒有解決方法或知道為什么,但清除緩存並再次運行它總是有效:

jest --clearCache

jest --coverage

我相信這可能與 ts-jest 而不是 jest 本身有關。

我得到了它的工作

包.json

...
"scripts": {
        ...
        "test": "jest --maxWorkers=1 --coverage"
...

jest.config.js

module.exports = {
    verbose: true,
    preset: 'ts-jest',
    testEnvironment: 'node',
    globals: {
        'ts-jest': {
            isolatedModules: true
        }
    },
    testPathIgnorePatterns: ['.d.ts', '.js'],
    collectCoverageFrom: [
        '**/*.ts',
        '!**/build/**',
        '!**/node_modules/**',
        '!**/vendor/**'
    ]
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM