Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Coverage for Typescript [closed]

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.

like image 971
Gerard Condon Avatar asked Jun 10 '13 15:06

Gerard Condon


People also ask

How do I check my code coverage?

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.

Does jest have code coverage?

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.

How do you ignore 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.


3 Answers

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.

like image 161
Ryan Cavanaugh Avatar answered Sep 22 '22 06:09

Ryan Cavanaugh


Update: August 2016

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:

Deep Map coverage

like image 40
McMath Avatar answered Sep 19 '22 06:09

McMath


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.

like image 31
Jesper Rønn-Jensen Avatar answered Sep 21 '22 06:09

Jesper Rønn-Jensen