Achieving 100% Condition Coverage in Jest with a TypeScript Function
This challenge focuses on understanding and achieving 100% condition coverage in Jest using a TypeScript function. Condition coverage ensures that every possible outcome of a boolean expression within your code is tested. This is a crucial aspect of robust testing, helping to identify potential bugs and ensure your code behaves as expected under all circumstances.
Problem Description
You are given a TypeScript function calculateDiscount that determines a discount amount based on a customer's order total and membership status. Your task is to write Jest tests that achieve 100% condition coverage for this function. This means ensuring that every if condition and its corresponding true and false branches are executed during your tests.
The calculateDiscount function is defined as follows:
function calculateDiscount(orderTotal: number, isMember: boolean): number {
let discount = 0;
if (orderTotal > 100) {
discount += 10;
}
if (isMember) {
discount += 5;
}
if (orderTotal >= 50 && orderTotal <= 100) {
discount += 2;
}
return discount;
}
Key Requirements:
- Write Jest tests that cover all possible execution paths within the
calculateDiscountfunction. - Achieve 100% condition coverage. Use Jest's coverage reports to verify this.
- Your tests should be clear, concise, and well-documented.
- The tests should handle various input combinations of
orderTotalandisMember.
Expected Behavior:
The tests should pass without errors and demonstrate that all conditions within the calculateDiscount function are exercised. The Jest coverage report should show 100% condition coverage.
Edge Cases to Consider:
orderTotalbeing exactly 100.orderTotalbeing less than 50.isMemberbeingtrueandfalse.- Combinations of
orderTotalandisMembervalues that trigger different conditions. orderTotalbeing 0.
Examples
Example 1:
Input: orderTotal = 150, isMember = true
Output: 15
Explanation: orderTotal > 100 is true (discount += 10), isMember is true (discount += 5).
Example 2:
Input: orderTotal = 75, isMember = false
Output: 2
Explanation: orderTotal > 100 is false, isMember is false, orderTotal >= 50 && orderTotal <= 100 is true (discount += 2).
Example 3:
Input: orderTotal = 25, isMember = true
Output: 0
Explanation: orderTotal > 100 is false, isMember is true, orderTotal >= 50 && orderTotal <= 100 is false.
Constraints
- You must use Jest as the testing framework.
- You must use TypeScript.
- The solution should be focused on achieving 100% condition coverage. While correctness is important, the primary goal is coverage.
- The tests should be reasonably efficient and avoid unnecessary complexity.
Notes
- Consider using a combination of test cases to cover all possible branches of the
ifstatements. - Jest's coverage reports are your friend! Use them to identify any uncovered conditions and adjust your tests accordingly.
- Think about the logical relationships between the conditions and how different input values will affect their outcomes.
- Don't overcomplicate the tests. Simple, focused tests are often the most effective.