0

I am trying to get what lines of code my unit tests do not cover.

I do my unit tests with mocha, which offers a reporter "json-cov" which should report how many lines I didn't execute.

I have two files, the first one (foo.js):

module.exports = function () {
    for (var result = 0, i = 0; i < arguments.length; i++) {
        result += arguments[i];
    }
    return result;
}

and the second one (test.js):

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);
    });
});

When I run mocha -R json-cov test.js I get the following result:

{
  "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
    }
  ]
}

What am I doing wrong, so that sloc, hits, misses and coverage are 0?

I also tried to use nodes require instead of jscs, without success.

EDIT: I just tried mocha -R json-cov test.js --coverage which results in an error if I use jscs require. When I use nodes require the result is the same as above.

EDIT: 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 jscoverage foo foo-cov which gave me an error abs source path or abs dest path needed!. I also tried absolute paths and a few other ways to arrange the arguments. No success. How can I prepare the files for jscoverage?

EDIT: If it is of any relevance, I am using Windows.

EDIT: 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.

EDIT: Got it to work. Instead of using a node package to prepare the code, I now just run jscoverage.exe (downloaded from here) from the console, and then go with mocha -R html-cov test.js > coverage.html. Now I have the problem that some code gets escaped. So I get

<span class="k">var</span> foo <span class="k">=</span> <span class="k">{</span>

instead of

var foo = {

with highlighting.

EDIT: The tags got escaped because they were rendered via a jade template with this code: td.source= line.source

Changing this to td.source!= line.source fixes this last issue I had.

2 Answers 2

3

The escaped code issue can be solved without having to edit mocha's jade template by using the "--no-highlight" option for jscoverage like so:

jscoverage --no-highlight foo foo-cov
Sign up to request clarification or add additional context in comments.

Comments

-1

Follow my EDITs to see what I did to solve this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.