Bernardo
Bernardo

Reputation: 1030

JavaScript code coverage in RequireJS / AMD modules

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?

Upvotes: 8

Views: 4003

Answers (5)

Alex Seville
Alex Seville

Reputation: 178

Blanket.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.

Upvotes: 6

Harald Rudell
Harald Rudell

Reputation: 837

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

Upvotes: 2

maraujop
maraujop

Reputation: 4594

Check out the project I've recently released for easy javascript in-browser coverage reports, JSCovReporter https://github.com/TwoApart/JSCovReporter

Upvotes: 2

Sebastian Golasch
Sebastian Golasch

Reputation: 686

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.

Upvotes: 2

Bernardo
Bernardo

Reputation: 1030

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.

Upvotes: 3

Related Questions