Hone logo
Hone
Problems

Jest Test Distribution Challenge: Ensuring Comprehensive Coverage

Writing effective tests is crucial for software quality, but simply having tests isn't enough. A well-distributed test suite ensures that different parts of your code are tested thoroughly, preventing regressions and uncovering hidden bugs. This challenge focuses on creating a Jest test distribution strategy to maximize test coverage and maintainability.

Problem Description

You are tasked with creating a Jest test suite for a simple utility function called calculateDiscount. This function takes a price and a discount percentage as input and returns the discounted price. Your goal is to design a test distribution that covers various scenarios, including positive prices, zero prices, zero discount, 100% discount, and potentially invalid inputs. The test suite should be well-organized, readable, and demonstrate a clear understanding of Jest's features for test distribution (e.g., describe, it, expect). The focus is on how you structure your tests, not just that they pass.

Key Requirements:

  • Comprehensive Coverage: Tests should cover a range of valid and edge-case inputs.
  • Clear Organization: Use describe blocks to group tests logically (e.g., by input type or scenario).
  • Descriptive Test Names: it blocks should have clear and concise descriptions of what they are testing.
  • Assertion Clarity: Use expect with appropriate matchers to verify the expected output.
  • Error Handling (Optional but Recommended): Consider how to test for potential errors or invalid input scenarios.

Expected Behavior:

The test suite should:

  1. Define a describe block for the calculateDiscount function.
  2. Within the describe block, create multiple it blocks, each testing a specific scenario.
  3. Each it block should use expect to assert that the output of calculateDiscount matches the expected value for the given input.
  4. The test suite should pass without errors, indicating that the calculateDiscount function behaves as expected across all tested scenarios.

Edge Cases to Consider:

  • Price is zero.
  • Discount is zero.
  • Discount is 100%.
  • Discount is greater than 100% (should it be handled? Define the expected behavior).
  • Price is a negative number (should it be handled? Define the expected behavior).
  • Discount is a negative number (should it be handled? Define the expected behavior).
  • Price or discount are not numbers (should it be handled? Define the expected behavior).

Examples

Example 1:

Input: price = 100, discount = 10
Output: 90
Explanation: A 10% discount on a price of 100 should result in a discounted price of 90.

Example 2:

Input: price = 50, discount = 0
Output: 50
Explanation: A 0% discount on a price of 50 should result in the original price of 50.

Example 3:

Input: price = 200, discount = 100
Output: 0
Explanation: A 100% discount on a price of 200 should result in a discounted price of 0.

Constraints

  • The calculateDiscount function is defined as follows:

    function calculateDiscount(price: number, discount: number): number {
      // Your implementation here (for testing purposes, assume it's correct)
      return price - (price * discount / 100);
    }
    
  • You are free to use any valid Jest matchers.

  • The test suite should be written in TypeScript.

  • Focus on the structure and distribution of your tests, not just passing them. A well-distributed test suite is more valuable than a suite that simply covers the happy path.

Notes

  • Think about how to group your tests logically to improve readability and maintainability.
  • Consider using beforeEach or afterEach hooks if they are helpful for setting up or cleaning up test data.
  • While error handling is optional, demonstrating how you would test for invalid inputs will be viewed favorably.
  • The goal is to create a robust and well-organized test suite that provides confidence in the correctness of the calculateDiscount function. Don't just test the obvious cases; think about the edge cases and potential failure points.
Loading editor...
typescript