title: JavaScript code coverage in RequireJS / AMD modules
body: <p>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)?</p>
  
  <p>Seems like a stupid question because of the prevalence of TDD approaches in the JS world and the take-over of AMD development.</p>
  
  <p>I've tried a million approaches, all are lacking. My project is a Backbone project with Jasmine Unit Tests:</p>
  
  <p>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.</p>
  
  <p>2) JSCoverage - total fail, doesn't like AMD modules.</p>
  
  <p>3) Chrome specific &amp; Firebug specific 'live' instrumenters - fail, don't like AMD modules.</p>
  
  <p>Every approach I try seems to require massive amounts of custom work.
  So maybe I need to spin a custom solution from scratch?</p>
  
  <p>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.</p>
  
  <p>Input?</p>