Running unit tests against an ES6+ source code base has now become an almost trivial task, thanks to Babel and all the ecosystem around it. There are a lot of good resources explaining how to do that, with different tools and frameworks.
On the other hand, code coverage on this kind of tests is a rather less covered subject …
If you apply regular code coverage solutions to that case, you end up with reports based on the transpiled code, not your original source code in ES6+, which isn’t really relevant …
Here comes isparta
This problem was solved by isparta, a code coverage tool for ES6+, using Babel, which provides code coverage reports using istanbul (which is also a code coverage tool … 😉 more infos here).
Using isparta, you can generate code coverage from unit tests on ES6+ source code base, directly against your original source code (not the transpiled one). This works great, combined with tools like karma-coverage, you can output coverage reports under any format (html, lcov … that can be processed by various tools/services like Jenkins or coveralls.io …).
isparta not maintained anymore
About two weeks ago, @duglasduteil added the “No Maintenance Intended” badge to his module. This should remind us that behind every open source project there are developers maintaining them (most of the time on there free time).
So, now may be the time to find a replacement for isparta … I was looking for an alternative for a few days when I eventually ended up on twitter with @kentcdodds who mentionned dtinth/babel-plugin-__coverage__.
ES6+ code coverage using a Babel plugin
Since Babel v6, you can make your very own plugins that can do much more than simply transpile ES6+ to ES5.
Babel is a generic multi-purpose compiler for JavaScript. More than that it is a collection of modules that can be used for many different forms of static analysis.
That’s what @dtinth has done with babel-plugin-__coverage__. He made a babel plugin that instrument your code, injecting metadata that will be processed by istambul (which is used under the hood by karma-coverage and other coverage report tools).
The great thing is that, since you go through Babel anyway to transpile your source code, the only thing you have to do to get those infos to feed tools like karma-coverage (or any istambul-based coverage reporter) is to activate this plugin in your .babelrc
file …
Using a babel plugin for coverage is a no-brainer.
You won’t have to add complex configuration or tools anymore. @dtinth‘s plugin is 300 LOC, it’s his first babel plugin that he made over two nights. As far as I have tested it, it works very well.
This is clear that using a babel plugin for that kind of purpose is the right way to proceed. Since it’s becoming easier to setup, we might see more projects using ES6+ including code coverage reports in a near futur.
You can see an example of setup on my project topheman/react-es6-redux.
Thanks for the article.
Now istanbul has released their own instrumenter [https://github.com/istanbuljs/babel-plugin-istanbul] based on the on @dtinth‘s plugin [babel-plugin-__coverage__]
I’ve ben unable to get babel-plugin-istanbul to work. Please write an article demonstrating a setup.
I first tried with mocha-webpack and then with mocha, but to no avail…
https://github.com/istanbuljs/nyc/issues/433
Thumbs up for the article and would give an amazing high five for an article demonstrating a setup with babel-plugin-istanbul.