Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

else path not taken in unit testing

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);
                });
            }
        }
like image 618
Ramesh Rajendran Avatar asked Nov 15 '19 12:11

Ramesh Rajendran


People also ask

How do you cover else path not taken in jest?

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

Which testing is not included in unit testing?

A test is not a unit-test if: it communicates with a database. it cannot run in parallel with other tests. uses the "environment" like registry or file system.

What are branches in jest coverage?

Branch coverage is a requirement that, for each branch in the program (e.g., if statements, loops), each branch have been executed at least once during testing. (It is sometimes also described as saying that each branch condition must have been true at least once and false at least once during testing.)

How does jest coverage work?

Jest is collecting coverage only on the function under tests, not from the entire project. This means that despite we are seeing 100% coverage here, potentially we are testing only a fraction of our code. Now Jest is identify correctly what needs to be tested.

How to create tests for parts that are not covered by unit tests?

If we click on CreateAppointmentService.ts we are able to see every line of code contained in that file. Lines highlighted in pink are statements that are not covered by the unit test. This helps us create tests for parts that are missing them.

How to test multiple inputs in a single unit test?

Unit testing frameworks can also provide a way for you to test multiple inputs through a single unit test by providing the inputs in a test method attribute. The fluent builder pattern isn’t specifically used for unit tests, but can come in handy during the arrange step as I’ll explain.

How do unit tests interact with the database?

When running the unit tests, the unit will fetch those objects and function in the same way as though it was interacting with a database.

How to test if an if/else condition is true or false?

Try writing a test that calls a function and passes in the values that would resolve falsy (or truthy) for each if/else condition. Take each message one at a time and write appropriate tests to do that.


2 Answers

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

like image 107
Philipp Meissner Avatar answered Sep 17 '22 19:09

Philipp Meissner


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

like image 38
Ashwani Panwar Avatar answered Sep 16 '22 19:09

Ashwani Panwar