15

I am working on angular 6

I don't have any else statement in my below code.But I am unable to cover branches due to else path not taken .What I need to do to get 100% branch coverage in this case?

 getHeaderDocumentList(documents: any) {
            if (documents) {
                documents.result.docAttachment.forEach(element => {
                    this.headerdocumentdetails.push(element.DocumentUniqueID);
                });
            }
        }
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234

2 Answers2

20

In order to get full coverage reported, the code needs to eventually hit the (here non-explicitly-existent) else-path. For that matter pass in a falsy parameter like 0 | false | undefined | null | NaN | '' | "" | `` as in

component.getHeaderDocumentList(false);
expect(false).toEqual(false); // This line is optional. Most test suites require some kind of assertion in each test.

Now your test-suite should report both branches as covered.

Last solution would be to add /* istanbul ignore else */ in before the if case. That will tell the coverage reporter to effectively ignore the else path

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59
  • Good Idea! But some case I am unable to fix this. Any way as of now 80% is enough. And one more question: did you heard anything about [sourceMap:true](https://github.com/webpack-contrib/style-loader/issues/271) – Ramesh Rajendran Nov 15 '19 at 13:48
  • Hm,that is rather weird. It should stop complaining once you ran the tests again. – Philipp Meissner Nov 15 '19 at 13:58
  • 10
    Last solution would be to add `/* istanbul ignore else */` in before the if case. That will tell the coverage reporter to effectively ignore the else path. – Philipp Meissner Nov 15 '19 at 13:59
  • If the expected output of a false/empty document list is that `headerdocumentdetails` does not change, that is a valid test. – Erik Philips Nov 16 '19 at 05:02
5

Just add /* istanbul ignore else */ before if statement which do not have else part. Coverage reporter will ignore the else path.

Ashwani Panwar
  • 3,819
  • 3
  • 46
  • 66