Pedro A
Pedro A

Reputation: 4323

Setting up Browserify with Istanbul in Karma with ES6+

I am trying to use Karma to run tests with accurate code coverage output for my files that use ES6+ including ES7 async/await syntax.

With only one source file: worked

As a start, I managed to make it work when I have only one source file (i.e. no require calls in my source files). I just told Karma to use babel on it and configured babel-plugin-istanbul on my .babelrc.

correct coverage output in html reporter

correct coverage output in text-summary reporter

With multiple source files with require(): didn't work

Now I want to make this work in my real situation, which is a lot of source files with require() calls from one to the other. To make this work, instead of simply using the babel preprocessor, I had to use the browserify preprocessor, but now it seems to be ignoring my .babelrc and not instrumenting the code with istanbul coverage stuff:

wrong coverage output in text-summary reporter

How can I fix this?


A repository for this question is available on GitHub

Since there's a lot going on, I decided to create a GitHub repo so that anyone can reproduce my situation instantly and easily:

Upvotes: 0

Views: 594

Answers (1)

Pedro A
Pedro A

Reputation: 4323

OP here. I figured it out:

npm install browserify-istanbul

Add this to karma.conf.js:

browserify: {
    configure: function(bundle) {
        bundle.transform(require('browserify-istanbul')({
            ignore: ['test/*.js']
        }));
    }
}

Leave the preprocessors as they were (no coverage preprocessor!)

preprocessors: {
    "test/*.js": [ "browserify" ],
    "lib/*.js": [ "browserify" ]
}

That's it. You can get rid of the .babelrc and the babel-plugin-istanbul, they are not even necessary.

Upvotes: 1

Related Questions