npm install karma-coverage-istanbul-reporter --save-dev
plugins: [ require('karma-jasmine'), require('karma-chrome-launcher'), require('karma-coverage-istanbul-reporter') // 添加这一行 ],
coverageIstanbulReporter: { reports: ['html', 'lcovonly', 'text-summary'], fixWebpackSourcePaths: true, thresholds: { statements: 80, lines: 80, branches: 80, functions: 80 }, include: ['src/app//*.component.ts'], // 添加要包含的文件路径 exclude: ['src//*.spec.ts', 'src/test.ts'] },
ng test --code-coverage
执行命令后,我们可以在coverage目录下找到覆盖率报告。只包含指定路径的文件。
//示例: coverage/ |--lcov-report/ |----src/ |------app/ |--------app.component.ts.html |--------app.module.ts.html |--------app-routing.module.ts.html |--------... |------... |----... |--coverage-final.json |--index.html
在这个示例中,我们只包含了src/app/目录下的所有组件。
我们可以在浏览器中打开coverage/index.html来查看报告。这里我们只能看到包含在“include”选项中的组件的报告。