简体   繁体   English

节点打字稿项目中的笑话覆盖率总是返回空

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

I am working on backend Typescript project when i am trying to get coverage report for unit test case,Jest returns empty coverage report in terminal as well as in html report stating nothing.当我试图获取单元测试用例的覆盖率报告时,我正在处理后端 Typescript 项目,Jest 在终端和 html 报告中返回空的覆盖率报告,没有说明任何内容。 i also tried with -- --coverage --watchAll=false but it also returns the empty document.我也试过-- --coverage --watchAll=false但它也返回空文档。 I could not able to found a solution for this issue.我无法找到解决此问题的方法。 can anyone help me to figureout what i am doing wrong here?谁能帮我弄清楚我在这里做错了什么?

package.json包.json

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

jest_unit.config.js 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
    }
  },
};

Folder Structure文件夹结构

|-- 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 and terminal shows no coverage lines Jest Coverage htm 和终端显示没有覆盖线

在此处输入图像描述

Can able to fix this issue by moving config file to root, Everything is good here in configuration, I dont know why jest behaves like this.可以通过将配置文件移动到 root 来解决这个问题,这里的配置一切都很好,我不知道为什么 jest 会这样。

Updated structure更新结构

Folder Structure文件夹结构

|-- 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

package.json包.json

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

Try changing your collectCoverageFrom as follows:尝试更改您的collectCoverageFrom如下:

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

The ** means to include all files, recursively. **表示递归地包含所有文件。 Details are at https://jestjs.io/docs/en/configuration#collectcoveragefrom-array详细信息位于https://jestjs.io/docs/en/configuration#collectcoveragefrom-array

I'm also facing that issue randomly, I don't have a fix or know why, but clearing the cache and running it again always works:我也随机面临这个问题,我没有解决方法或知道为什么,但清除缓存并再次运行它总是有效:

jest --clearCache

jest --coverage

I believe this might be related to ts-jest not jest itself.我相信这可能与 ts-jest 而不是 jest 本身有关。

I got it working with this我得到了它的工作

package.json包.json

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

jest.config.js 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