CodePark/ questions / Coverage, mutation testing & CI
START PRACTICINGPRACTICE
Backend question · Backend testing › Coverage, mutation testing & CI

What line and branch coverage does this test give for fee()?

function fee(amount, isVip) {
  let f = amount * 0.02;
  if (isVip) f = 0;
  return f;
}

test('VIPs pay no fee', () => {
  expect(fee(100, true)).toBe(0);
});
100% lines, 50% branches

The answer is 100% lines, 50% branches. Every line runs, because the condition is true and the if body is on the same line. Branch coverage counts both outcomes of the if: true and false. The non-VIP path (false) never ran. That is exactly where a fee bug could hide.