Code Coverage for Typescript using karma-jasmine and istanbul

Udit S. Ahlawath picture Udit S. Ahlawath · Nov 26, 2015 · Viewed 19k times · Source

I am trying to get the Code Coverage for my typescript Code in karma framework using Istanbul in karma.conf typescript files are included and by karma typescript-preprocessor we able to do unit testing and code coverage of the typescript code but Code coverage report come for trans piled JavaScript code

How can I get the coverage report for typescript code?

Here is my karma.conf file.

Answer

Erik Barke picture Erik Barke · Sep 3, 2016

Install karma-typescript:

npm install karma-typescript --save-dev

Put this in your karma.conf.js:

frameworks: ["jasmine", "karma-typescript"],

files: [
    { pattern: "src/**/*.ts" }
],

preprocessors: {
    "**/*.ts": ["karma-typescript"]
},

reporters: ["progress", "karma-typescript"],

browsers: ["Chrome"]

This will run your Typescript unit tests on the fly and generate Istanbul html coverage that look like this:

To run the above example you need to install a few packages:

npm install @types/jasmine jasmine-core karma karma-chrome-launcher karma-cli karma-jasmine karma-typescript typescript

This is the complete configuration for unit testing vanilla Typescript code, no tsconfig.json needed in this case. For more complex setups with Angular, React etc you can find examples in the examples folder and in the integration tests.