What is a Branch in angularjs code coverage

A branch is where the runtime can choose whether it can take one path or another. Lets take the following example:
if(a) {
    Foo();
}

if(b) {
    Bar();
}

Yay();
When reaching the first line, it can decide if it wants to go inside the body of the if(a)-statement. Also, it can decide not to do that. At this stage, we've seen two paths already (one branch).
The next statement after that gets more interesting. It can go inside the if body and execute Bar. It can also not do that. But remember that we've already had a branch before. The result may vary if Foo was called or not.
So we end up with four possible paths:
  • Not calling Foo, not calling Bar either
  • Calling Foo, not calling Bar
  • Not calling Foo, calling Bar
  • Calling both Foo and Bar
The last Yay is always executed, no matter if Foo or Bar was called, so that doesn't count as a branch. So the code snippet above contains four paths / two branches.
Like other answers already have mentioned, there are numerous statements that can cause a branch (if/switch). Don't forget conditional-loops, such as while/for/do-while though.
The code coverage tool wants to make sure that you've tested all branches. Best would be if all paths have been tested, not just the branches. This, to make sure that no unwanted behavior is executed.

Comments

Popular posts from this blog

How to Choose a Technology Stack for Web Application Development

[Advance Java] Difference Between Apache and Tomcat

Setting ESLint on a React Typescript project