gotwarlost / istanbul

Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests and browser tests. Built for scale.
Other
8.7k stars 787 forks source link

Cannot get report for non-covered parts of conditions #784

Open ozum opened 7 years ago

ozum commented 7 years ago

Hi,

I use mocha & istanbul for some projects and lab for some others.

For the very simplified example below istanbul reported 100% coverage whereas lab reported less than 100%.

Istanbul

$ istanbul cover _mocha

const Code    = require('code');
const expect  = Code.expect;

const util = require('../util');

describe('deneme', () => {
  it('should return true', (done) => {
    expect(util.deneme('a')).to.true();
    done();
  });

  it('should return false', (done) => {
    expect(util.deneme()).to.false();
    done();
  });
});

Lab

$ lab --coverage --reporter html > report.html

const Lab  = require('lab');
const Code = require('code');

const lab      = Lab.script();
const describe = lab.describe;
const it       = lab.it;
const expect   = Code.expect;

const util = require('../util');

exports.lab = lab;
describe('deneme', () => {
  it('should return true', (done) => {
    expect(util.deneme('a')).to.true();
    done();
  });

  it('should return false', (done) => {
    expect(util.deneme('b')).to.false();
    done();
  });
});

Function

// util.js file
function deneme(a) {
  return (a && a.startsWith('a')) ? true : false;
}

module.exports = {
  deneme
};

Although two branches are executed by tests and as a result tests seem to cover 100% of the code, second part of the condition is never tested/executed.

lab reported that first part of the condition a is always true, so that second part a.startWith('a') is never executed. Please see screenshot below:

screen shot 2017-03-23 at 16 31 14

A third test should be added for this case:

  it('should return false for undefined', (done) => {
    expect(util.deneme()).to.false();
    done();
  });

Not in this simple example, but in more complex situations it helped me hunt some hard to catch bugs.

Is it possible to detect condition coverage in Istanbul?

Kind Regards,

ozum commented 7 years ago

I got confused, but I see that project continues under another repo. I moved my question to https://github.com/istanbuljs/nyc/issues/538.