节点打字稿项目中的笑话覆盖率始终返回空 [英] Jest coverage in a node typescript project always returns empty

查看:63
本文介绍了节点打字稿项目中的笑话覆盖率始终返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试获取单元测试用例的覆盖率报告时,我正在从事后端Typescript项目,Jest在终端以及html报告中均未声明任何内容,返回空的覆盖率报告.我还尝试了---coverage --watchAll = false ,但它也返回了空文档.我无法找到此问题的解决方案.有人可以帮我弄清楚我在做什么错吗?

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. i also tried with -- --coverage --watchAll=false but it also returns the empty document. 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

"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,终端没有显示覆盖线

Jest Coverage htm and terminal shows no coverage lines

推荐答案

能够通过将配置文件移至根目录来解决此问题,这里的配置一切都很好,我不知道为什么玩笑会这样.

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.

更新后的结构

文件夹结构

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

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

这篇关于节点打字稿项目中的笑话覆盖率始终返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆