Short and seemingly stupid question because so simple and you'd think ubiquitous: has anyone gotten any kind of code coverage to work within a RequireJS front-end project (non NodeJS)?
Seems like a stupid question because of the prevalence of TDD approaches in the JS world and the take-over of AMD development.
I've tried a million approaches, all are lacking. My project is a Backbone project with Jasmine Unit Tests:
1) JSTD with Coverage plugin. JSTD has trouble properly loading and instrumenting AMD modules. If I run JSTD on a single js file (combined by RequireJS optimizer) then code coverage works beautifully, EXCEPT that coverage is then collected and metrics defined on the ENTIRE file. Great, that's useless because that includes 3rd party libraries, and because I can't target a single unit to help test development. Argh.
2) JSCoverage - total fail, doesn't like AMD modules.
3) Chrome specific & Firebug specific 'live' instrumenters - fail, don't like AMD modules.
Every approach I try seems to require massive amounts of custom work. So maybe I need to spin a custom solution from scratch?
How about the following approach: expand the RequireJS optimizer to instrument code and create coverage. It would be in-memory instrumentation which can be enabled with a require.config({ instrument: true }) flag. Every time require loads a module it automatically instruments it and places it in its module repository. Coverage statistics are collected in an object underneath the global require object and accessible from anywhere, say, after a Jasmine test-run.
Input?
Short and seemingly stupid question because so simple and you'd think ubiquitous: has anyone gotten any kind of code coverage to work within a RequireJS front-end project (non NodeJS)?
Seems like a stupid question because of the prevalence of TDD approaches in the JS world and the take-over of AMD development.
I've tried a million approaches, all are lacking. My project is a Backbone project with Jasmine Unit Tests:
1) JSTD with Coverage plugin. JSTD has trouble properly loading and instrumenting AMD modules. If I run JSTD on a single js file (combined by RequireJS optimizer) then code coverage works beautifully, EXCEPT that coverage is then collected and metrics defined on the ENTIRE file. Great, that's useless because that includes 3rd party libraries, and because I can't target a single unit to help test development. Argh.
2) JSCoverage - total fail, doesn't like AMD modules.
3) Chrome specific & Firebug specific 'live' instrumenters - fail, don't like AMD modules.
Every approach I try seems to require massive amounts of custom work. So maybe I need to spin a custom solution from scratch?
How about the following approach: expand the RequireJS optimizer to instrument code and create coverage. It would be in-memory instrumentation which can be enabled with a require.config({ instrument: true }) flag. Every time require loads a module it automatically instruments it and places it in its module repository. Coverage statistics are collected in an object underneath the global require object and accessible from anywhere, say, after a Jasmine test-run.
Input?
Share Improve this question edited Oct 10, 2013 at 23:58 David Harkness 36.5k11 gold badges116 silver badges136 bronze badges asked Jun 16, 2012 at 16:18 BernardoBernardo 1,0301 gold badge12 silver badges22 bronze badges 2- code coverage is non trivial to implement. You'll have to do AST hackery with something like bunker. – Raynos Commented Jun 19, 2012 at 13:41
- Agreed. That's why I've started by using existing AST & instrumentation libraries (Esprima, Node-Cover). I have most of the RequireJS code modifications done and am working on the instrumentation & coverage collection part. – Bernardo Commented Jun 19, 2012 at 14:57
5 Answers
Reset to default 6Blanket.js works with Qunit using a modified RequireJS loader. We're working on Jasmine support, and will be happy to accept any feedback or suggestions.
If your project is going into production, Blanket.js isn't for you, but if it's just a side project it might be worth seeing if it meets your needs.
EDIT: Blanket now supports Jasmine.
Well it turns out Node-Coverage actually provides an AMD agnostic interface which works just fine with RequireJS and Jasmine, by serving the code to be instrumented from its own server.
I'll still spend some time working on the RequireJS coverage implementation I started, since it wouldn't require running a separate server.
Whats exactly your problem with AMD modules and JSCoverage? I have a setup (QUnit/PhantomJS/JSCoverage) running with AMD modules.
Maybe you just forgot to setup your library paths correct in your require.config?
After i changes the library paths to use the instrumented code, instead of the "normal" files, everything worked.
Check out the project I've recently released for easy javascript in-browser coverage reports, JSCovReporter https://github.com/TwoApart/JSCovReporter
mochawrapper
I just wrote a node module that automates coverage reporting. It is based on mocha, jscoverage and node's assert. You do not have to modify require or use make or enviornment variables.
It is hosted on github: mochawrapper
jscoverage maybe does not run in the browser, so you may want to look at: bunker which is based on uglify-js