title: Code coverage in unit tests
body: <p>I am trying to get what lines of code my unit tests do not cover.</p>
  
  <p>I do my unit tests with mocha, which offers a reporter "json-cov" which should report how many lines I didn't execute.</p>
  
  <p>I have two files, the first one (foo.js):</p>
  
  <pre><code>module.exports = function () {
      for (var result = 0, i = 0; i &lt; arguments.length; i++) {
          result += arguments[i];
      }
      return result;
  }
  </code></pre>
  
  <p>and the second one (test.js):</p>
  
  <pre><code>var expect = require('expect.js'),
      jsc = require('jscoverage'),
      //foo = require('./foo.js');
      foo = jsc.require(module, './foo.js');
  
  describe('foo', function () {
      it('should add all arguments', function () {
          expect(foo(1,1)).to.be(2);
      });
  });
  </code></pre>
  
  <p>When I run <code>mocha -R json-cov test.js</code> I get the following result:</p>
  
  <pre><code>{
    "instrumentation": "node-jscoverage",
    "sloc": 0,
    "hits": 0,
    "misses": 0,
    "coverage": 0,
    "files": [],
    "stats": {
      "suites": 1,
      "tests": 1,
      "passes": 1,
      "pending": 0,
      "failures": 0,
      "start": "2013-01-30T18:00:15.785Z",
      "end": "2013-01-30T18:00:15.787Z",
      "duration": 2
    },
    "tests": [
      {
        "title": "should add all arguments",
        "fullTitle": "foo should add all arguments",
        "duration": 1
      }
    ],
    "failures": [],
    "passes": [
      {
        "title": "should add all arguments",
        "fullTitle": "foo should add all arguments",
        "duration": 1
      }
    ]
  }
  </code></pre>
  
  <p>What am I doing wrong, so that sloc, hits, misses and coverage are 0?</p>
  
  <p>I also tried to use nodes <code>require</code> instead of jscs, without success.</p>
  
  <p><strong>EDIT:</strong> I just tried <code>mocha -R json-cov test.js --coverage</code> which results in an error if I use jscs <code>require</code>. When I use nodes <code>require</code> the result is the same as above.</p>
  
  <p><strong>EDIT:</strong> I can't even run jscoverage from console. I created a folder foo and foo-cov and copied my foo.js in the folder foo. Then I ran <code>jscoverage foo foo-cov</code> which gave me an error <code>abs source path or abs dest path needed!</code>. I also tried absolute paths and a few other ways to arrange the arguments. No success. How can I prepare the files for jscoverage?</p>
  
  <p><strong>EDIT:</strong> If it is of any relevance, I am using Windows.</p>
  
  <p><strong>EDIT:</strong> Just realized that there isn't only a single 'jscoverage' package available via npm, but also a 'visionmedia-jscoverage'. Trying to install that one fails. Probably because of Windows vs. Linux.</p>
  
  <p><strong>EDIT:</strong> Got it to work. Instead of using a node package to prepare the code, I now just run jscoverage.exe (downloaded from <a href="http://siliconforks.com/jscoverage/" rel="nofollow">here</a>) from the console, and then go with <code>mocha -R html-cov test.js &gt; coverage.html</code>. Now I have the problem that some code gets escaped. So I get</p>
  
  <pre><code>&lt;span class="k"&gt;var&lt;/span&gt; foo &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="k"&gt;{&lt;/span&gt;
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>var foo = {
  </code></pre>
  
  <p>with highlighting.</p>
  
  <p><strong>EDIT:</strong> The tags got escaped because they were rendered via a jade template with this code: <code>td.source= line.source</code></p>
  
  <p>Changing this to <code>td.source!= line.source</code> fixes this last issue I had.</p>