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);
});
}
}
Just add /* istanbul ignore else */ before if statement which do not have else part. Coverage reporter will ignore the else path.
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.
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.)
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.
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.
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.
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.
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.
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
Just add /* istanbul ignore else */
before if
statement which do not have else
part. Coverage reporter will ignore the else path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With