Node.js 开玩笑说通过了测试,但--覆盖了不拾取文件的选项

Node.js 开玩笑说通过了测试,但--覆盖了不拾取文件的选项,node.js,typescript,jestjs,babel-jest,ts-jest,Node.js,Typescript,Jestjs,Babel Jest,Ts Jest,问题描述: 我已经为一个typescript类编写了两个测试。这两个测试都通过了,因此jest成功地检索了测试文件。然后我使用了--coverage选项,但jest似乎没有在这里选择覆盖的文件。 以下是我得到的输出: api_jester | PASS src/tests/repositories/user.test.ts api_jester | User Repository api_jester | ✓ it should return an empty ar

问题描述:
我已经为一个typescript类编写了两个测试。这两个测试都通过了,因此jest成功地检索了测试文件。然后我使用了--coverage选项,但jest似乎没有在这里选择覆盖的文件。
以下是我得到的输出:

api_jester    | PASS src/tests/repositories/user.test.ts
api_jester    |   User Repository
api_jester    |     ✓ it should return an empty array (18ms)
api_jester    |     ✓ should successfully create a user and return its data (7ms)
api_jester    | 
api_jester    | ----------|----------|----------|----------|----------|-------------------|
api_jester    | File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
api_jester    | ----------|----------|----------|----------|----------|-------------------|
api_jester    | All files |        0 |        0 |        0 |        0 |                   |
api_jester    | ----------|----------|----------|----------|----------|-------------------|
api_jester    | Test Suites: 1 passed, 1 total
api_jester    | Tests:       2 passed, 2 total
api_jester    | Snapshots:   0 total
api_jester    | Time:        3.208s
api_jester    | Ran all test suites.
我尝试过使用CollectionCoverage选项,但没有成功。我已经用在github上找到的一些简单示例测试了覆盖,这些示例都很有效,所以问题不在于我的环境。我猜我不知怎么错过了我的配置中的一些东西,但我在这方面花费了太多时间,我感到有点沮丧,所以也许一些新的外观可以帮助

项目架构:

config
|__ jest.config.js
|__ tsconfig.json
src
|__tests
|  |__repositories
|     |__user.test.ts
|__repositories
   |___ userRepository
        |__User.ts
module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  roots: ["../src/tests/"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  collectCoverageFrom: ["../src/"],
  moduleFileExtensions: ["ts", "js", "json"],
  coverageDirectory: "../coverage"
};
Jest.config.js:

config
|__ jest.config.js
|__ tsconfig.json
src
|__tests
|  |__repositories
|     |__user.test.ts
|__repositories
   |___ userRepository
        |__User.ts
module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  roots: ["../src/tests/"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  collectCoverageFrom: ["../src/"],
  moduleFileExtensions: ["ts", "js", "json"],
  coverageDirectory: "../coverage"
};
package.json

{
  "name": "theralog_api",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "build": "tsc",
    "prettier": "npx prettier --write src/**/*.ts --config ./config/.prettierrc",
    "eslint": "npx eslint --config ./config/.eslintrc ./src/**/**/*",
    "start:dev": "npx nodemon -L --config ./config/api.nodemon.json",
    "test:watch": "npx nodemon -L --config ./config/jester.nodemon.json",
    "test:coverage": "npx jest --config ./config/jest.config.js --coverage --colors --watch"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/compression": "^1.0.1",
    "@types/express": "^4.17.1",
    "@types/graphql-depth-limit": "^1.1.2",
    "@types/jest": "^24.0.23",
    "@types/node": "^12.7.12",
    "@typescript-eslint/eslint-plugin": "^2.5.0",
    "@typescript-eslint/parser": "^2.5.0",
    "apollo-server-testing": "2.9.7",
    "babel-jest": "^24.9.0",
    "eslint": "^6.5.1",
    "eslint-config-prettier": "^6.4.0",
    "graphql-depth-limit": "^1.1.0",
    "graphql-import": "^0.7.1",
    "graphql-import-node": "0.0.4",
    "jest": "^24.9.0",
    "nodemon": "^1.19.3",
    "prettier": "^1.18.2",
    "ts-jest": "^24.1.0",
    "ts-node": "^8.4.1",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.7.2"
  },
  "dependencies": {
    "apollo-server-express": "^2.9.6",
    "compression": "^1.7.4",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "graphql": "^14.5.8",
    "http": "0.0.0",
    "lodash": "^4.17.15",
    "ncp": "^2.0.0",
    "pg": "^7.12.1",
    "winston": "3.2.1"
  }
}
{
  "watch": ["../src"],
  "ext": "ts",
  "exec": "npx jest --config ./config/jest.config.js --watchAll"
}
jester.nodemon.json

{
  "name": "theralog_api",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "build": "tsc",
    "prettier": "npx prettier --write src/**/*.ts --config ./config/.prettierrc",
    "eslint": "npx eslint --config ./config/.eslintrc ./src/**/**/*",
    "start:dev": "npx nodemon -L --config ./config/api.nodemon.json",
    "test:watch": "npx nodemon -L --config ./config/jester.nodemon.json",
    "test:coverage": "npx jest --config ./config/jest.config.js --coverage --colors --watch"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/compression": "^1.0.1",
    "@types/express": "^4.17.1",
    "@types/graphql-depth-limit": "^1.1.2",
    "@types/jest": "^24.0.23",
    "@types/node": "^12.7.12",
    "@typescript-eslint/eslint-plugin": "^2.5.0",
    "@typescript-eslint/parser": "^2.5.0",
    "apollo-server-testing": "2.9.7",
    "babel-jest": "^24.9.0",
    "eslint": "^6.5.1",
    "eslint-config-prettier": "^6.4.0",
    "graphql-depth-limit": "^1.1.0",
    "graphql-import": "^0.7.1",
    "graphql-import-node": "0.0.4",
    "jest": "^24.9.0",
    "nodemon": "^1.19.3",
    "prettier": "^1.18.2",
    "ts-jest": "^24.1.0",
    "ts-node": "^8.4.1",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^3.7.2"
  },
  "dependencies": {
    "apollo-server-express": "^2.9.6",
    "compression": "^1.7.4",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "graphql": "^14.5.8",
    "http": "0.0.0",
    "lodash": "^4.17.15",
    "ncp": "^2.0.0",
    "pg": "^7.12.1",
    "winston": "3.2.1"
  }
}
{
  "watch": ["../src"],
  "ext": "ts",
  "exec": "npx jest --config ./config/jest.config.js --watchAll"
}

您在jest.config.js,
collectCoverage:true

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  roots: ["../src/tests/"],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  collectCoverage: true,
  collectCoverageFrom: ["../src/"],
  moduleFileExtensions: ["ts", "js", "json"],
  coverageDirectory: "../coverage"
};
我还使用了更具描述性的CollectionCoverage:

collectCoverageFrom: [
    '<rootDir>/src/**/*.ts',
    '!<rootDir>/src/**/*.interface.ts',
    '!<rootDir>/src/**/*.mock.ts',
    '!<rootDir>/src/**/*.module.ts',
    '!<rootDir>/src/**/*.spec.ts',
    '!<rootDir>/src/**/*.test.ts',
    '!<rootDir>/src/**/*.d.ts'
],
CollectCoverage来自:[
“/src/***.ts”,
“!/src/***.interface.ts”,
“!/src/***.mock.ts”,
“!/src/***.module.ts”,
“!/src/***.spec.ts”,
“!/src/***.test.ts”,
“!/src/***.d.ts”
],
这样我就排除了一些我不想计算覆盖率的文件,比如我的模块、模拟和测试

我的完整文件包含原始Jest init进程和来自该进程的注释

有关每个配置属性的详细说明,请访问:

module.exports={
//测试中所有导入的模块都应自动模拟
//automock:false,
//在第一次失败后停止运行测试
//保释金:错,
//解析模块时尊重package.json中的“browser”字段
//浏览器:false,
//Jest应在其中存储其缓存的依赖项信息的目录
//缓存目录:“C:\\Users\\sCott\\AppData\\Local\\Temp\\jest”,
//自动清除每个测试之间的模拟调用和实例
//clearMocks:错误,
//指示在执行测试时是否应收集覆盖率信息
报道:是的,
//一组全局模式,指示应为其收集覆盖率信息的一组文件
收款人:[
“/src/***.ts”,
“!/src/***.mock.ts”,
“!/src/***.module.ts”,
“!/src/***.spec.ts”,
“!/src/***.test.ts”,
“!/src/***.d.ts”
],
//Jest应在其中输出其覆盖率文件的目录
Coverage目录:“/docs”,
//用于跳过覆盖率集合的regexp模式字符串数组
Coverage PathIgnorePatterns:[
“\\\\node\u模块\\\\”
],
//Jest在撰写报道时使用的记者姓名列表
搬运工:[
“lcov”,
“三叶草”,
“文本摘要”
],
//为覆盖率结果配置最小阈值强制的对象
//coverageThreshold:null,
//使调用不推荐的API时抛出有用的错误消息
错误:是的,
//使用全局模式数组从忽略的文件强制收集覆盖率
//ForceCoverage匹配:[],
//导出异步函数的模块路径,该异步函数在所有测试套件之前触发一次
//全局设置:空,
//导出异步函数的模块的路径,该异步函数在所有测试套件之后触发一次
//全局搜索:空,
//需要在所有测试环境中可用的一组全局变量
全球:{
“开玩笑”:{
“诊断”:错误,
“tsConfig”:“tsConfig.json”
}
},
//从所需模块的位置向上递归搜索的目录名数组
//模块目录:[
//“节点_模块”
// ],
//模块使用的文件扩展名数组
moduleFileExtensions:[
“ts”,
“tsx”,
“js”
],
//从正则表达式到模块名的映射,允许使用单个模块来删除资源
//moduleNameMapper:{},
//regexp模式字符串的数组,在模块加载器认为“可见”之前与所有模块路径匹配
//modulePathIgnorePatterns:[],
//激活测试结果的通知
//通知:错误,
//指定通知模式的枚举。需要{notify:true}
//notifyMode:“始终”,
//用作Jest配置基础的预设
//预设值:空,
//从一个或多个项目运行测试
//项目:空,
//使用此配置选项将自定义报告器添加到Jest
//记者:不明确,,
//在每次测试之间自动重置模拟状态
//重置模拟:错误,
//在运行每个单独的测试之前重置模块注册表
//重置模块:错误,
//自定义解析程序的路径
//解析程序:null,
//在每次测试之间自动恢复模拟状态
//restoreMocks:false,
//Jest应在其中扫描测试和模块的根目录
//rootDir:null,
//Jest用于在中搜索文件的目录路径列表
根:[
“/src”
],
//允许您使用自定义运行程序而不是Jest的默认测试运行程序
//跑步者:“开玩笑的跑步者”,
//指向模块的路径,这些模块在每次测试之前运行一些代码来配置或设置测试环境
//设置文件:[],
//模块的路径,该模块在每次测试之前运行一些代码来配置或设置测试框架
//SetupTestFrameworkScript文件:null,
//Jest应用于快照测试的快照序列化程序模块的路径列表
//快照序列化程序:[],
//将用于测试的测试环境
测试环境:“节点”,
//将传递到测试环境的选项
//测试环境选项: