We've just started a project in Typescript and we need to get code coverage figures.
Our existing JavaScript projects use Instanbul in Grunt for coverage. We are unsure how to replicate this for TypeScript.
Are there any tools for generating code coverage from the TypeScript code itself? Or do we run the Istanbul (or similar) tool against the generated JavaScript code.
To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.
As part of the output Jest generates, you'll see not only the test results but also the code coverage report. Since the current code is very simple, you should be getting 100 percent code coverage.
The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.
On the TypeScript team, we just use regular code coverage tools on the compiled JavaScript. We've found this to be more than sufficient, since usually for code coverage you are looking at total coverage % (which doesn't change significantly) or are deep-diving at expression-level (which also doesn't change significantly).
If you found a tool that supported it (I'm not aware of any yet), you could in theory use the source maps emitted by the compiler to map the coverage data back to the TypeScript code. It's probably not worth the trouble.
It is now possible to run Istanbul against TypeScript source code using Istanbul v1 (currently in the alpha stage) along with TypeScript Node.
The following assumes that you are using Mocha as a test framework and that all test code is under the standard test/
directory.
First, install the requisite packages:
npm install --save-dev mocha ts-node npm install --save-dev --save-exact [email protected]
Then include something like the following in your package.json
:
"scripts": { "test": "istanbul cover -e .ts _mocha -- --compilers ts:ts-node/register" }
That's it. Run npm test
and you'll be covered.
See my Deep Map project for a working example in which the test files are kept in the same directory as the source code. Here is a sample of the HTML output:
Two years after this question originally was posted, there is now remap-istanbul
which seems promising.
you can read more about it in Sitepen: Code Coverage for TypeScript and Other Transpiled Languages
As they write in the Github project:
A package that provides the ability to remap Istanbul code coverage information to its original source positions based on a JavaScript Source Maps v3.
As I read the documentation, the project will take your istanbul-generated coverage as input for conversion based on the sourcemap. This sounds like an additional step, but I am sure it will benefit so that you can get rid of those transpiled autogenerated lines in the coverage report.
I believe this is exactly what you will need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With