Hone logo
Hone
Problems

Implementing Incremental Testing with Jest

Incremental testing, also known as bottom-up testing, involves building and testing small, isolated units of code before integrating them into larger components. This approach allows for early detection of errors and simplifies debugging. This challenge focuses on implementing incremental testing principles within a Jest testing environment for a simple utility function.

Problem Description

You are tasked with creating a utility function called calculateDiscountedPrice that calculates the discounted price of an item based on its original price and a discount percentage. You need to implement a Jest test suite that utilizes incremental testing to ensure the function's correctness. The test suite should start with testing the most basic scenarios and progressively add more complex cases, ensuring each addition doesn't break existing functionality. You should focus on writing small, focused tests that verify specific aspects of the function's behavior.

What needs to be achieved:

  1. Implement the calculateDiscountedPrice function in TypeScript.
  2. Create a Jest test suite for the function.
  3. Structure the test suite using an incremental testing approach, starting with simple cases and gradually adding complexity.
  4. Ensure that each new test case is added without breaking existing tests.

Key Requirements:

  • The calculateDiscountedPrice function should accept two arguments: price (number) and discountPercentage (number).
  • The discountPercentage should be a value between 0 and 100 (inclusive).
  • The function should return the discounted price as a number, rounded to two decimal places.
  • If the discountPercentage is 0, the function should return the original price.
  • If the discountPercentage is 100, the function should return 0.
  • The test suite should include tests for:
    • Zero discount
    • 100% discount
    • A simple discount (e.g., 10%)
    • A discount with a non-integer price (e.g., price = 19.99, discount = 20%)
    • A discount with a large price and discount percentage (e.g., price = 1000, discount = 50%)

Expected Behavior:

The calculateDiscountedPrice function should correctly calculate the discounted price based on the provided inputs. The Jest test suite should pass all tests, demonstrating the function's correctness and the effectiveness of the incremental testing approach.

Edge Cases to Consider:

  • Invalid input types (e.g., string instead of number). While the function itself doesn't need to explicitly handle these, the tests should ideally cover them to ensure robustness.
  • Negative price or discount percentage (the function should handle these gracefully, potentially returning an error or a default value).
  • Very large numbers that could lead to precision issues.

Examples

Example 1:

Input: calculateDiscountedPrice(100, 10)
Output: 90.00
Explanation: 10% of 100 is 10, so the discounted price is 100 - 10 = 90.

Example 2:

Input: calculateDiscountedPrice(50, 0)
Output: 50.00
Explanation: A 0% discount means no discount is applied.

Example 3:

Input: calculateDiscountedPrice(25.50, 50)
Output: 12.75
Explanation: 50% of 25.50 is 12.75, so the discounted price is 25.50 - 12.75 = 12.75.

Example 4:

Input: calculateDiscountedPrice(1000, 100)
Output: 0.00
Explanation: A 100% discount means the price is reduced to 0.

Constraints

  • The price and discountPercentage arguments must be numbers.
  • The discountPercentage must be between 0 and 100 (inclusive).
  • The returned discounted price must be rounded to two decimal places.
  • The test suite should be written using Jest and TypeScript.
  • The solution should be well-structured and easy to understand.

Notes

  • Start with the simplest test cases first (e.g., zero discount).
  • Add new test cases incrementally, ensuring that existing tests continue to pass.
  • Consider using beforeEach or afterEach hooks in Jest to set up and tear down test environments if necessary.
  • Focus on writing small, focused tests that verify specific aspects of the function's behavior.
  • Think about how to structure your tests to make them readable and maintainable. Grouping tests by functionality can be helpful.
  • While explicit type checking within the function isn't strictly required, consider how you might add it for increased robustness.
Loading editor...
typescript