Measuring Line Coverage with Jest in TypeScript
Line coverage is a crucial metric for assessing the thoroughness of your tests. This challenge asks you to implement a simple function and then write Jest tests to achieve a high line coverage percentage. Understanding and utilizing line coverage helps ensure your code is well-tested and reduces the risk of introducing bugs.
Problem Description
You are tasked with creating a TypeScript function called calculateDiscount that determines the discount amount based on the purchase total. The function should handle different discount tiers based on the total amount and return the calculated discount. You must then write Jest tests to achieve a line coverage of at least 90% for the calculateDiscount function. This includes covering all branches (if/else statements) within the function.
What needs to be achieved:
- Implement the
calculateDiscountfunction according to the specifications below. - Write Jest tests that cover all lines of code in
calculateDiscount. - Verify that the Jest report shows a line coverage of at least 90%.
Key Requirements:
- The
calculateDiscountfunction must accept atotalargument (number). - The function should return a number representing the discount amount.
- The discount tiers are as follows:
- If
totalis less than 100, no discount (0). - If
totalis between 100 and 500 (inclusive), a 10% discount. - If
totalis greater than 500, a 20% discount.
- If
- The function must be written in TypeScript.
- Tests must be written using Jest.
Expected Behavior:
calculateDiscount(50)should return0.calculateDiscount(200)should return20.calculateDiscount(600)should return120.
Edge Cases to Consider:
totalbeing 0.totalbeing exactly 100 or 500.- Negative
totalvalues (handle gracefully - return 0).
Examples
Example 1:
Input: calculateDiscount(50)
Output: 0
Explanation: The total is less than 100, so no discount is applied.
Example 2:
Input: calculateDiscount(200)
Output: 20
Explanation: The total is between 100 and 500, so a 10% discount is applied (200 * 0.10 = 20).
Example 3:
Input: calculateDiscount(600)
Output: 120
Explanation: The total is greater than 500, so a 20% discount is applied (600 * 0.20 = 120).
Example 4:
Input: calculateDiscount(-10)
Output: 0
Explanation: Negative total values should result in no discount.
Constraints
- The
totalinput can be any number (integer or float). - The discount amount returned must be a number.
- The Jest tests must achieve a line coverage of at least 90%.
- The
calculateDiscountfunction should be concise and readable.
Notes
- Focus on achieving high line coverage. Consider using
ifstatements and testing all possible branches. - Use Jest's built-in reporting features to verify the line coverage percentage. You can typically view the report in the terminal after running your tests or in a browser window.
- Think about how to structure your tests to efficiently cover all lines of code. Consider using
describeblocks to organize your tests logically. - Remember to install Jest and TypeScript if you haven't already. You'll likely need to configure Jest to work with TypeScript.