Here is a piece of code that maps specific fields from one object to another and copies their values:
//Function parameters
var source = {"f1": "v1", "f2": "v2", "f3":"v3"};
var fieldsMapping = {"f1": "c1", "f2":"c2"};
//Function definition begins
var copiedObj = {};
for (var name in source) {
if (source.hasOwnProperty(name)) { //Line X
if(fieldsMapping[name]){
copiedObj[fieldsMapping[name]] = source[name];
}
}
}
console.log(copiedObj); //outputs {c1: "v1", c2: "v2"}
I have created a test case for this function using jest
, achieving 100% line coverage. However, branch coverage indicates that Line X
is not covered. According to standards such as those found in SonarSource and TSLint, a for-in
loop should be accompanied by an if condition
.
Can anyone provide suggestions on how to create a test case that improves branch coverage
for this scenario?