Hone logo
Hone
Problems

Jest Function Coverage: Ensuring Comprehensive Testing

Function coverage in Jest is a crucial aspect of robust testing. This challenge focuses on implementing a Jest test suite that accurately measures and reports the line coverage of a given TypeScript function. Achieving high function coverage helps ensure that your code is thoroughly tested and minimizes the risk of unexpected behavior.

Problem Description

You are provided with a TypeScript function calculateDiscount that determines a discount amount based on the purchase total and a discount code. Your task is to create a Jest test suite that achieves 100% line coverage for this function. This means every line of code within calculateDiscount must be executed by at least one test case.

The calculateDiscount function accepts two arguments: total (a number representing the purchase total) and discountCode (a string representing the discount code). It returns a number representing the discount amount. The function has several branches based on the discountCode value.

Key Requirements:

  • Write Jest tests that cover all possible branches within the calculateDiscount function.
  • Ensure 100% line coverage is achieved.
  • Use meaningful test descriptions to clearly indicate what each test case is verifying.
  • The tests should be written in TypeScript.

Expected Behavior:

The Jest test suite should pass without any failures or pending tests. The coverage report generated by Jest should indicate 100% line coverage for the calculateDiscount function.

Edge Cases to Consider:

  • total being zero or negative.
  • discountCode being an empty string.
  • discountCode having unexpected values (e.g., null, undefined, or values not defined in the function logic).
  • Invalid input types for total and discountCode. (While the function doesn't explicitly handle these, consider how your tests might expose potential issues).

Examples

Example 1:

Input: total = 100, discountCode = "SUMMER20"
Output: 20
Explanation: The discount code "SUMMER20" provides a 20% discount on a total of 100, resulting in a discount of 20.

Example 2:

Input: total = 50, discountCode = "WELCOME10"
Output: 5
Explanation: The discount code "WELCOME10" provides a 10% discount on a total of 50, resulting in a discount of 5.

Example 3:

Input: total = 200, discountCode = ""
Output: 0
Explanation: An empty discount code results in no discount.

Constraints

  • The calculateDiscount function is provided below. You are not allowed to modify this function.
  • You must use Jest and TypeScript.
  • The test suite must achieve 100% line coverage of the calculateDiscount function.
  • Tests should be concise and readable.

Notes

  • Use Jest's expect function to assert the expected discount amount.
  • Consider using describe blocks to group related tests.
  • Pay close attention to the different branches within the calculateDiscount function and ensure each branch is tested.
  • Run jest --coverage to verify the coverage report.
// calculateDiscount.ts
export function calculateDiscount(total: number, discountCode: string): number {
  if (total <= 0) {
    return 0;
  }

  switch (discountCode) {
    case "SUMMER20":
      return total * 0.2;
    case "WELCOME10":
      return total * 0.1;
    case "LOYALTY5":
      return total * 0.05;
    default:
      return 0;
  }
}
Loading editor...
typescript