Hone logo
Hone
Problems

Achieving 100% Branch Coverage with Jest in TypeScript

This challenge focuses on understanding and achieving 100% branch coverage in a TypeScript project using Jest. Branch coverage ensures that every possible path through your code (determined by conditional statements like if, else if, else, switch, and loops) is executed during testing. This is crucial for robust and reliable software.

Problem Description

You are given a TypeScript file containing a function that determines the shipping cost based on the order total and customer location. The function includes conditional logic to handle different shipping rates. Your task is to write Jest tests that achieve 100% branch coverage for this function. This means your tests must cover all possible outcomes of the conditional statements within the function.

The function calculateShippingCost takes two arguments: orderTotal (a number representing the total cost of the order) and customerLocation (a string representing the customer's location, which can be "domestic" or "international"). It returns a number representing the shipping cost.

Key Requirements:

  • Write Jest tests that cover all branches of the calculateShippingCost function.
  • Ensure that the tests are well-structured and easy to understand.
  • The tests should verify that the correct shipping cost is returned for different order totals and customer locations.
  • The tests should achieve 100% branch coverage as reported by Jest.

Expected Behavior:

The calculateShippingCost function should behave as follows:

  • If the customer location is "domestic":
    • If the order total is less than $50, the shipping cost is $5.
    • If the order total is $50 or greater, the shipping cost is free ($0).
  • If the customer location is "international":
    • If the order total is less than $100, the shipping cost is $20.
    • If the order total is $100 or greater, the shipping cost is $10.

Edge Cases to Consider:

  • Invalid customer location (although the function doesn't explicitly handle this, ensure your tests don't inadvertently miss branches due to unexpected input).
  • Zero or negative order totals (although the function doesn't explicitly handle this, consider how your tests might interact with these values).

Examples

Example 1:

Input: orderTotal = 40, customerLocation = "domestic"
Output: 5
Explanation: Order total is less than $50 and customer is domestic, so shipping cost is $5.

Example 2:

Input: orderTotal = 60, customerLocation = "domestic"
Output: 0
Explanation: Order total is $50 or greater and customer is domestic, so shipping cost is $0.

Example 3:

Input: orderTotal = 80, customerLocation = "international"
Output: 20
Explanation: Order total is less than $100 and customer is international, so shipping cost is $20.

Example 4:

Input: orderTotal = 120, customerLocation = "international"
Output: 10
Explanation: Order total is $100 or greater and customer is international, so shipping cost is $10.

Constraints

  • The calculateShippingCost function is provided below. You are not allowed to modify it.
  • You must use Jest for testing.
  • The tests must achieve 100% branch coverage.
  • The tests should be written in TypeScript.

Notes

  • Use Jest's branch coverage reporting feature to verify that you have achieved 100% coverage. Run jest --coverage to see the coverage report.
  • Think carefully about the different branches in the calculateShippingCost function and how to write tests to exercise each one.
  • Consider using a combination of test cases to cover all possible scenarios.
  • Focus on writing clear and concise tests that are easy to understand and maintain.
// calculateShippingCost.ts
export function calculateShippingCost(orderTotal: number, customerLocation: string): number {
  if (customerLocation === "domestic") {
    if (orderTotal < 50) {
      return 5;
    } else {
      return 0;
    }
  } else if (customerLocation === "international") {
    if (orderTotal < 100) {
      return 20;
    } else {
      return 10;
    }
  } else {
    return 0; // Default case for unknown locations
  }
}
Loading editor...
typescript