Achieving 100% Branch Coverage with Jest
Writing comprehensive tests is crucial for ensuring the robustness and reliability of your code. While line coverage tells you if a line of code was executed, branch coverage goes a step further by verifying that every possible decision point (like if statements, switch cases, and logical operators) in your code has been taken down at least once. This challenge will guide you through implementing branch coverage for a given TypeScript function using Jest.
Problem Description
Your task is to write Jest tests for a provided TypeScript function such that the tests achieve 100% branch coverage for that function. You will need to analyze the function's logic and design test cases that exercise all possible branches of its conditional statements.
Requirements:
- Implement Jest Tests: Write a set of Jest tests in TypeScript for the given function.
- Achieve 100% Branch Coverage: Ensure that every conditional branch within the function is executed by your test suite.
- Use Jest: All tests must be written using the Jest testing framework.
- TypeScript: The solution (tests) should be written in TypeScript.
Expected Behavior:
When you run your Jest tests with coverage enabled (e.g., jest --coverage), the report should indicate 100% branch coverage for the target function.
Edge Cases to Consider:
- Combinations of conditions in
ifstatements (true/false for each part of an&&or||). - All possible cases in a
switchstatement. - Handling of unexpected or boundary input values.
Examples
Let's consider a simple function for demonstration.
Target Function:
// src/example.ts
export function categorizeNumber(num: number): string {
if (num > 10) {
if (num % 2 === 0) {
return "Large Even";
} else {
return "Large Odd";
}
} else if (num < 0) {
return "Negative";
} else {
return "Small or Zero";
}
}
Example Test Scenario (Illustrative - not the full solution):
To achieve branch coverage for categorizeNumber, we need to hit all the if and else branches.
- Branch 1:
num > 10is true- Sub-branch 1.1:
num % 2 === 0is true (e.g.,categorizeNumber(12)-> "Large Even") - Sub-branch 1.2:
num % 2 === 0is false (e.g.,categorizeNumber(13)-> "Large Odd")
- Sub-branch 1.1:
- Branch 2:
num > 10is false ANDnum < 0is true (e.g.,categorizeNumber(-5)-> "Negative") - Branch 3:
num > 10is false ANDnum < 0is false (e.g.,categorizeNumber(5)-> "Small or Zero")
A complete test suite would include individual test cases for each of these scenarios.
Constraints
- The provided target function will be a single TypeScript file.
- The number of conditional branches will be manageable, typically not exceeding 10-15 distinct branches.
- The input values for testing will be standard TypeScript
numbertypes.
Notes
- You will need to set up a Jest environment for TypeScript. If you're unsure, consider using
ts-jest. - Run your Jest tests with the
--coverageflag to see the coverage report. - Analyze the coverage report carefully to identify any branches that haven't been covered and add tests accordingly.
- Focus on the logic of the function and how to trigger different paths, rather than complex input validation or external dependencies.
- The goal is to demonstrate understanding of how to achieve branch coverage, not to find obscure edge cases that might not be logically intended.