Last Updated: September 09, 2019
·
4.449K
· avgp

Code coverage with Testacular and Jasmine

Setting up the coverage reporter

Testacular (version >= 0.5.3) has a coverage reporter to generate differnent formats of coverage reports (lcov, html, text,..).
To set it up, you can either init a new testacular.conf.js with

$ testacular init

(or use your existing testacular.conf.js) and edit it to preprocess ("instrument") the javascript files for coverage discovery and add the "coverage" reporter like this:

preprocessors = {
  '**/app/assets/javascripts/*.js': 'coverage',
  ...
}

reporters = ['progress', 'coverage'];

Be really careful with the "preprocessors" option, because it uses minimatch in a certain way, that requires the "**/" to be at the very beginning of each path you specify. Running testacular with --log-level debug tells you which files have or haven't been picked up by the preprocessor, if you're in doubt.

For more options see the testacular wiki page for the coverage reporter on GitHub

Pitfall: Manual browser mode

If you get weird results or errors thrown at you, check the browser setting:

browsers = [];

means that you need to manually point your browser to http://localhost:SOMEPORT and testacular will execute within that browser.
For some reason I couldn't nail down, this screws you up big time.
Don't do it. Be a good boy, specify your browsers there, such as

browsers = ['Chrome', 'Firefox'];

and you'll be good.

7 Responses
Add your response

Any ideas how to test code coverage on Coffeescript projects? I've been using instanbul with karma for quite a long time but it doesn't seem they're going to add .coffee support soon.

over 1 year ago ·

@paprikkastudio :
did you try to add a preprocessor in karma.conf.js :


preprocessors = {
'**/app/**/*.js': 'coverage',
'**/*.coffee': 'coffee'
};
</pre>
over 1 year ago ·

@laurentroger yeah, I have, but thanks for taking the time answering me. Turned out to be an npm issue. Now everything works pretty well.

over 1 year ago ·

hey I have the same problem that I can't use the coffescript with coverage could you please share the solution !

over 1 year ago ·

hey I have the same problem that I can't use the coffescript with coverage could you please share the solution !

over 1 year ago ·
over 1 year ago ·

@mohitleo9 : can you describe your problem on stackoverflow and send back the link ?

over 1 year ago ·