Jest Execution Path Control: Branch Coverage Challenge
This challenge focuses on mastering Jest's capabilities for controlling execution paths within your tests, specifically targeting branch coverage. Understanding how to force Jest to execute different branches of your code is crucial for ensuring thorough testing and robust software. You'll be writing tests that explicitly trigger different conditional branches within a given function.
Problem Description
You are given a TypeScript function calculateDiscount that determines a discount based on a customer's order total and membership status. The function has multiple conditional branches that need to be tested to achieve full branch coverage. Your task is to write a Jest test suite that covers all possible execution paths within calculateDiscount. This means you need to write tests that trigger each branch of the if and else if statements within the function.
The calculateDiscount function is defined as follows:
function calculateDiscount(orderTotal: number, isMember: boolean): number {
if (orderTotal > 100) {
if (isMember) {
return 0.2; // 20% discount for members with orders over $100
} else {
return 0.1; // 10% discount for non-members with orders over $100
}
} else if (orderTotal > 50) {
if (isMember) {
return 0.05; // 5% discount for members with orders between $50 and $100
} else {
return 0; // No discount for non-members with orders between $50 and $100
}
} else {
return 0; // No discount for orders $50 or less
}
}
Key Requirements:
- Write a Jest test suite with multiple test cases.
- Each test case should cover a specific execution path within
calculateDiscount. - Ensure that all branches of the
ifandelse ifstatements are executed at least once during the test suite. - Use Jest's assertion methods to verify the returned discount value for each test case.
- The test suite should achieve 100% branch coverage.
Expected Behavior:
The test suite should pass without any errors, indicating that all branches of the calculateDiscount function have been executed and the returned values are as expected. Jest's branch coverage report should confirm 100% coverage.
Edge Cases to Consider:
- Order total exactly equal to 100 or 50.
isMemberbeingtrueandfalsefor various order totals.- Order total less than 50.
Examples
Example 1:
Input: orderTotal = 120, isMember = true
Output: 0.2
Explanation: Order total is greater than 100, and the customer is a member, so a 20% discount is applied.
Example 2:
Input: orderTotal = 75, isMember = false
Output: 0
Explanation: Order total is greater than 50 but not greater than 100, and the customer is not a member, so no discount is applied.
Example 3:
Input: orderTotal = 30, isMember = true
Output: 0
Explanation: Order total is less than 50, regardless of membership status, no discount is applied.
Constraints
- The
calculateDiscountfunction must not be modified. - You must use Jest for testing.
- The solution must be written in TypeScript.
- The test suite should be concise and readable.
- Performance is not a primary concern for this challenge; focus on achieving full branch coverage.
Notes
- Think about the different combinations of
orderTotalandisMemberthat will trigger each branch. - Use meaningful test case names to clearly indicate which branch is being tested.
- Consider using Jest's
expectfunction to assert the returned discount value. - Run Jest with the
--coverageflag to verify branch coverage. This is essential to confirm you've met the requirements.