I successfully implemented testing using Karma and Webpack for my Typescript sandbox project. The code coverage data is gathered by Istanbul Instrumenter Loader, but I am facing an issue where the reported coverage of 100% is misleading because it only includes modules imported in the tests.
While looking for a solution, I came across a section in the readme of Istanbul Instrumenter Loader on Github:
To generate a code coverage report for all components (including untested ones), you need to import both all sources and tests.
test/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 imports everything from index files within the source directory. My main query is: how can I translate this snippet to Typescript without relying on the import * from *
workaround? Or is there a more efficient approach?
Edit
I found a related question on importing all files from a folder in Typescript here. Does this imply that I need an index.ts
file where I manually import each module whenever a new one is added? There must be a better way.
Edit 2
If anyone knows of tools that can generate coverage reports for the entire codebase while supporting Typescript + Webpack + Karma + Mocha, I would appreciate your suggestions. I tried using nyc but was unable to get any code coverage results.
Edit 3
Here's the translation of the above index.js
snippet into Typescript:
declare const require: any;
const ctx = require.context('../src', true, /\.ts$/);
ctx.keys().map(ctx);
Edit 4
A new karma plugin named karma-sabarivka-reporter
has been developed to address the coverage statistics issue. Refer to the accepted answer for more information.