title: Karma tests: measure coverage for untested code
body: <p>I have successfully set up testing with Karma and Webpack for my <a href="https://github.com/hoefling/chrome-extension-typescript" rel="noreferrer">sandbox project written in Typescript</a>. The code coverage metrics are collected by Istanbul Instrumenter Loader. What bothers me is that <a href="https://coveralls.io/github/hoefling/chrome-extension-typescript" rel="noreferrer">I get the coverage reported only for the modules that are being imported in the tests</a>, so the reported coverage of 100% is in fact a dirty lie.</p>
  <p>Looking for a solution, I have found a passage in Istanbul Instrumenter Loader's <a href="https://github.com/webpack-contrib/istanbul-instrumenter-loader/blob/master/README.md" rel="noreferrer">readme</a>:</p>
  <blockquote>
  <p>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.</p>
  <h3>test/index.js</h3>
  <pre class="lang-javascript prettyprint-override"><code>// 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);
  </code></pre>
  </blockquote>
  <p>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? Or is there a better solution that does not require the <code>import * from *</code> workaround?</p>
  <h3>Edit</h3>
  <p>I've found this question: <a href="https://stackoverflow.com/questions/38158429/typescript-1-8-modules-import-all-files-from-folder">Typescript 1.8 modules: import all files from folder</a>. Does this mean that I need an <code>index.ts</code> file where I have to import each module? This would mean that each time I introduce a new module file, I have to manually add its import to <code>index.ts</code>? There must be a better way.</p>
  <h3>Edit 2</h3>
  <p>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. I have tried <a href="https://github.com/istanbuljs/nyc" rel="noreferrer"><strong>nyc</strong></a>, but couldn't manage to get any code coverage at all.</p>
  <h3>Edit 3</h3>
  <p>Here's the <code>index.js</code> from above translated to Typescript:</p>
  <pre><code>declare const require: any;
  
  const ctx = require.context('../src', true, /\.ts$/);
  ctx.keys().map(ctx);
  </code></pre>
  <h3>Edit 4</h3>
  <p>There's a karma plugin now called <code>karma-sabarivka-reporter</code> that corrects the coverage statistics. Check out the <a href="https://stackoverflow.com/a/59011983/2650249">accepted answer</a> for details.</p>