18

How do you setup karma test runner to generate code coverage reports of a typescript project?

Given the following folder structure and karma.conf.js file I'm already using karma to run my tests written in TypeScript.

I already fiddled around with karma-coverage and remap-istanbul but without any luck yet. If possible I'd like to do it without any additional npm scripts.

.
├── karma.conf.js
├── package.json
├── src
│   └── add.ts
├── test
│   └── addSpec.ts
├── tsconfig.json
├── typings
│   ├── globals
│   └── index.d.ts
└── typings.json

karma.conf.js

var istanbul = require('browserify-istanbul');

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['mocha', 'chai', 'sinon', 'browserify'],
    files: [
      'test/**/*Spec.ts'
    ],
    exclude: [
    ],
    preprocessors: {
      'test/**/*Spec.ts': ['browserify']
    },
    browserify: {
      debug: true,
      plugin: ['tsify'],
      transform: [
        istanbul({irgnore: ['**/node_modules/**']})
      ]
    },
    reporters: ['progress', 'coverage']
  })
}

Update 1:

I made some progress by adding browserify-istanbul to the setup. I think the overall metrics are fine but the source file view is a bit odd.

source file view

addSpec.ts

import { add } from '../src/add'
const expect = chai.expect

describe('test add module', () => {
  it('should add 2 numbers', () => {
    expect(add(2, 2)).to.be.equal(4)
  })
})

Update 2:

Until today I could not figure out a way to create an "integrated" karma setup with browserify and typescript. Nevertheless I have a different solution that is working for me.

karma.conf.js

module.exports = function(config) {
  config.set({
    basePath: '',
    frameworks: ['source-map-support', 'mocha'],
    files: [
      'test/**/*Spec.ts'
    ],
    exclude: [],
    preprocessors: {
      'test/**/*Spec.ts': ['webpack']
    },
    webpack: {
      devtool: 'inline-source-map',
      resolve: {
        extensions: ['', '.ts', '.js']
      },
      module: {
        loaders: [
          { test: /\.ts$/, exclude: /node_modules/, loader: 'ts-loader', query: { compilerOptions: { inlineSourceMap: true }} }
        ],
        postLoaders: [
          { test: /\.ts$/, include: /src/, loader: 'istanbul-instrumenter' }
        ]
      }
    },
    webpackMiddleware: {
      noInfo: true
    },
    reporters: ['progress', 'coverage'],
    coverageReporter: {
      dir: 'coverage',
      reporters: [
        { type: 'json', subdir: '.', file: 'coverage.json' }
      ]
    },
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Electron'],
    singleRun: false,
    concurrency: Infinity
  })
}

package.json

{
  ...
  "scripts": {
    "test": "rimraf coverage && karma start --single-run && npm run coverage",
    "coverage": "npm run coverage:remap && npm run coverage:report",
    "coverage:remap": "remap-istanbul -i coverage/coverage.json -o coverage/coverage.json -t json",
    "coverage:report": "istanbul report html"
  },
  ...
}
Oliver
  • 978
  • 6
  • 17
  • I run coverage daily for my project without any issue. To start with, You need to put your source code location (not test) under "preprocessor". Your "files" too should have source code location.. If it still doesn't work then please put some details on what error you are facing. – TypeScripter Aug 04 '16 at 14:44
  • Would you mind to make a gist out of your working karma.conf.js file? – Oliver Aug 04 '16 at 14:55
  • I am using Jasmine and angular-CLi so the details will be different. Listing down only relevant details.. plugins:['karma-coverage'], files:[ {pattern: 'src/**/*.js', included: false, watched: true}, {pattern: 'test/**/*.js, included: false, watched: true}], preprocessors: {'src/**/*.js': 'coverage'}, reporters: ['progress','coverage'], coverageReporter: { type: 'html'., dir:'coverage/' – TypeScripter Aug 04 '16 at 15:27
  • sorry for the mess above.. using mobile so can't format it that easy.. please try and have you karma.conf.js updated in you questions.. I will point it out if there is some small mistakes (if any) – TypeScripter Aug 04 '16 at 15:28
  • nice, did my answer help ? in that case I will make it an answer.. moreover, what is odd about source file view? – TypeScripter Aug 04 '16 at 17:16
  • @TypeScripter No, not really. We have a somewhat different setup. Your angular-cli config looks for .js files but I want karma to compile and bundle my .ts files before it runs the tests. – Oliver Aug 04 '16 at 18:43
  • good to know.. so are you saying that in preprocessors section you still have test files and not source file? – TypeScripter Aug 04 '16 at 19:03
  • @TypeScripter I don't think it's necessary to add the source files to the preprocessors object. The test files act as an entry point browserify and since the source files are imported/required by the tests they are processed either way. – Oliver Aug 04 '16 at 19:45
  • sure. Thank you for all information. I was following karma-coverage documentation which specifically asks not to put any test files there.https://github.com/karma-runner/karma-coverage – TypeScripter Aug 04 '16 at 19:53

0 Answers0