简体   繁体   English

Karma 测试:测量未测试代码的覆盖率

[英]Karma tests: measure coverage for untested code

I have successfully set up testing with Karma and Webpack for my sandbox project written in Typescript .我已经成功地为我用 Typescript 编写的沙箱项目设置了 Karma 和 Webpack 测试。 The code coverage metrics are collected by Istanbul Instrumenter Loader.代码覆盖率指标由Istanbul Instrumenter Loader 收集。 What bothers me is that I get the coverage reported only for the modules that are being imported in the tests , so the reported coverage of 100% is in fact a dirty lie.令我烦恼的是, 我只报告了测试中导入的模块的覆盖率,因此报告的 100% 覆盖率实际上是一个肮脏的谎言。

Looking for a solution, I have found a passage in Istanbul Instrumenter Loader's readme :寻找解决方案,我在Istanbul Instrumenter Loader的自述文件中找到了一段:

To create a code coverage report for all components (even for those for which you have no tests yet) you have to require all the 1) sources and 2) tests.要为所有组件(即使对于那些还没有测试的组件)创建代码覆盖率报告,您必须需要所有 1) 源和 2) 测试。

test/index.js测试/index.js

 // requires all tests in `project/test/src/components/**/index.js` const tests = require.context('./src/components/', true, /index\\.js$/); tests.keys().forEach(tests); // requires all components in `project/src/components/**/index.js` const components = require.context('../src/components/', true, /index\\.js$/); components.keys().forEach(components);

If I understand correctly, this snippet walks over all index files in source dir and imports everything from them.如果我理解正确,此代码段会遍历源目录中的所有索引文件并从中导入所有内容。 My question is: how to correctly translate this snippet to Typescript?我的问题是:如何正确地将此代码段转换为 Typescript? Or is there a better solution that does not require the import * from * workaround?或者是否有更好的解决方案不需要import * from *解决方法?

Edit编辑

I've found this question: Typescript 1.8 modules: import all files from folder .我发现了这个问题: Typescript 1.8 modules: import all files from folder Does this mean that I need an index.ts file where I have to import each module?这是否意味着我需要一个index.ts文件来导入每个模块? This would mean that each time I introduce a new module file, I have to manually add its import to index.ts ?这意味着每次我引入一个新的模块文件时,我都必须手动将其导入添加到index.ts There must be a better way.一定会有更好的办法。

Edit 2编辑 2

I'm also open to other tools that can generate coverage report for the whole code base, the only condition them being able to cope with the Typescript + Webpack + Karma + Mocha stack.我也对其他可以为整个代码库生成覆盖率报告的工具持开放态度,这是它们能够处理 Typescript + Webpack + Karma + Mocha 堆栈的唯一条件。 I have tried nyc , but couldn't manage to get any code coverage at all.我试过nyc ,但根本无法获得任何代码覆盖率。

Edit 3编辑 3

Here's the index.js from above translated to Typescript:这是上面的index.js翻译成 Typescript:

declare const require: any;

const ctx = require.context('../src', true, /\.ts$/);
ctx.keys().map(ctx);

Edit 4编辑 4

There's a karma plugin now called karma-sabarivka-reporter that corrects the coverage statistics.现在有一个名为karma-sabarivka-reporter的 karma 插件可以纠正覆盖率统计数据。 Check out the accepted answer for details.查看已接受的答案以了解详细信息。

IMPORTANT:重要的:

This answer will not solve the above problem, it's fundamentally wrong.这个答案并不能解决上述问题,从根本上是错误的。 See comments.看评论。


NYC supports coverage for untested code with --all flag. NYC 支持使用--all标志覆盖未经测试的代码。 Assuming your test command in package.json is假设你在package.jsontest命令是

"test": "karma start karma.conf.js",

You can test coverage after npm install -D nyc ts-node , you can add the following command and run it.您可以在npm install -D nyc ts-node之后测试覆盖率,您可以添加以下命令并运行它。 This command expects the source code to check for coverage to be in src directory.此命令要求源代码检查src目录中的覆盖率。

"coverage": "nyc --all --include src --extension .ts --require ts-node/register npm test",
  • --all flag is used to check all files for coverage. --all标志用于检查所有文件的覆盖范围。
  • --extension .ts is for checking all TypeScript files, you can add more extension like --extension .ts --extension .tsx . --extension .ts用于检查所有 TypeScript 文件,您可以添加更多扩展名,如--extension .ts --extension .tsx
  • --include src is for the directory to check for coverage. --include src用于检查覆盖范围的目录。
  • -- require ts-node/register is to teach NYC to understand TypeScript. -- require ts-node/register是教 NYC 理解 TypeScript。

After that you should see all .ts files being included.之后,您应该会看到包含的所有 .ts 文件。

Note: Using ts-node/register may cause issues with line numbers on reports.注意:使用ts-node/register可能会导致报告中的行号出现问题。 To solve this you will probably need to register source map support as well..要解决这个问题,您可能还需要注册源映射支持

For projects tested using Karma + Istanbul as coverage reporter – I've just created karma plugin, which adds all the files in the project to coverage statistic - https://github.com/kopach/karma-sabarivka-reporter .对于使用 Karma +Istanbul 作为覆盖报告器进行测试的项目 - 我刚刚创建了 karma 插件,它将项目中的所有文件添加到覆盖率统计 - https://github.com/kopach/karma-sabarivka-reporter

To use it -> install npm install --save-dev karma-sabarivka-reporter使用它 -> 安装npm install --save-dev karma-sabarivka-reporter

And update karma.conf.js like this:并像这样更新karma.conf.js

reporters: [
  // ...
  'sabarivka'
  // 'coverage-istanbul' or 'coverage' (reporters order is important for 'coverage' reporter)
  // ...
],
coverageReporter: {
  include: [
      // Specify include pattern(s) first
      'src/**/*.(ts|js)',
      // Then specify "do not touch" patterns (note `!` sign on the beginning of each statement)
      '!src/main.(ts|js)',
      '!src/**/*.spec.(ts|js)',
      '!src/**/*.module.(ts|js)',
      '!src/**/environment*.(ts|js)'
  ]
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM