Implementing Modified Condition/Decision Coverage (MC/DC) in Jest
Achieving high code coverage is crucial for software quality, but simply reaching a certain percentage isn't enough. Modified Condition/Decision Coverage (MC/DC) is a more rigorous testing standard that ensures each condition within a decision is tested independently, and each decision is exercised with all possible outcomes. This challenge asks you to implement a Jest test suite that demonstrates and achieves MC/DC coverage for a given TypeScript function.
Problem Description
You are provided with a TypeScript function calculateDiscount that determines a discount based on customer type and purchase amount. Your task is to create a Jest test suite that achieves MC/DC coverage for this function. This means your tests must ensure that:
- Each condition within a decision is tested independently. For example, if a decision involves two conditions (A and B), you must have tests that exercise the decision where A is true and B is false, A is false and B is true, A is true and B is true, and A is false and B is false.
- Each decision is exercised with all possible outcomes. This means testing both the 'true' and 'false' branches of each
ifstatement or other decision points.
The calculateDiscount function is defined as follows:
function calculateDiscount(customerType: string, purchaseAmount: number): number {
let discount = 0;
if (customerType === 'premium') {
if (purchaseAmount > 100) {
discount = 0.2; // 20% discount for premium customers with large purchases
} else {
discount = 0.1; // 10% discount for premium customers with smaller purchases
}
} else if (customerType === 'regular') {
if (purchaseAmount > 50) {
discount = 0.05; // 5% discount for regular customers with larger purchases
}
}
return discount;
}
Expected Behavior:
- The test suite should pass without errors.
- The Jest coverage report should demonstrate that MC/DC coverage has been achieved for the
calculateDiscountfunction. This means all conditions within all decisions are independently tested, and all decision outcomes are exercised. - The tests should be well-structured and readable, clearly demonstrating the intent of each test case.
Examples
Example 1:
Input: customerType = 'premium', purchaseAmount = 150
Output: 0.2
Explanation: The function applies the 20% discount because the customer is premium and the purchase amount is greater than 100.
Example 2:
Input: customerType = 'premium', purchaseAmount = 50
Output: 0.1
Explanation: The function applies the 10% discount because the customer is premium and the purchase amount is not greater than 100.
Example 3:
Input: customerType = 'regular', purchaseAmount = 75
Output: 0.05
Explanation: The function applies the 5% discount because the customer is regular and the purchase amount is greater than 50.
Example 4:
Input: customerType = 'regular', purchaseAmount = 25
Output: 0
Explanation: The function applies no discount because the customer is regular and the purchase amount is not greater than 50.
Example 5:
Input: customerType = 'guest', purchaseAmount = 100
Output: 0
Explanation: The function applies no discount because the customer is not premium or regular.
Constraints
- You must use Jest as the testing framework.
- You must use TypeScript.
- The solution must achieve MC/DC coverage for the
calculateDiscountfunction. - The test suite should be concise and avoid unnecessary complexity.
- The code should be well-formatted and readable.
Notes
- Consider the different combinations of
customerTypeandpurchaseAmountthat need to be tested to achieve MC/DC coverage. - Think about how to structure your tests to clearly demonstrate the independent testing of each condition.
- Use Jest's assertion methods to verify the expected discount values.
- You can use Jest's coverage reporting features to verify that MC/DC coverage has been achieved. Run
jest --coverageto see the report. Pay close attention to the condition coverage within theifstatements.