首页 > 解决方案 > Jest 和 Vue 的覆盖率报告不起作用

问题描述

我正在使用 Jest 对我的 Vue 项目进行单元测试,我在控制台中获得了代码覆盖率,那里没有问题,但是 HTML 报告不起作用 -

在此处输入图像描述

包.json:

...
"unit": "jest --config test/unit/jest.conf.js --coverage",
...

jest.conf.js:

const path = require('path')

module.exports = {
  verbose: true,
  testURL: "http://localhost/",
  rootDir: path.resolve(__dirname, '../../'),
  moduleFileExtensions: [
    'js',
    'json',
    'vue'
  ],
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1'
  },
  transform: {
    '^.+\\.js$': '<rootDir>/node_modules/babel-jest',
    '.*\\.(vue)$': '<rootDir>/node_modules/vue-jest'
  },
  testPathIgnorePatterns: [
    '<rootDir>/test/e2e'
  ],
  snapshotSerializers: ['<rootDir>/node_modules/jest-serializer-vue'],
  setupFiles: ['<rootDir>/test/unit/setup'],
  mapCoverage: true,
  coverageDirectory: '<rootDir>/test/unit/coverage',
  collectCoverageFrom: [
    'src/**/*.{js,vue}',
    '!src/main.js',
    '!src/router/index.js',
    '!src/plugins/vuetify.js',
    '!**/node_modules/**'
  ]
}

这可能是什么原因?先感谢您。

标签: vue.jsjestjs

解决方案


看起来您的覆盖率报告属性可能需要调整。您应该尝试包括html在您的 中coverageReporters,如下所示:

module.exports = {
  ...
  coverageReporters: ['html']
  ...
}

推荐阅读