szkxsw
Last Updated: February 25, 2016
·
3.539K
· g33konaut
D38ff2119d6ace825b4c7b64e6f422d8

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.

Say Thanks
Respond

7 Responses
Add your response

6426
Photo

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 ·
7764
278f68fa8885c19d43b591812d330417

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


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

@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 ·
8916
8665d02d0860a8a505b5790c79af90cb

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 ·
8917
8665d02d0860a8a505b5790c79af90cb

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 ·
8918
8665d02d0860a8a505b5790c79af90cb
over 1 year ago ·
8919
278f68fa8885c19d43b591812d330417

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

over 1 year ago ·